Gunicorn Documentation, Release 19.10.0
3. Port your application to use aiohttp’s web.Application API.
4. Use the aiohttp.worker.GunicornWebWorker worker instead of the deprecated gaiohttp worker.
2.9.2 Choosing a Worker Type
The default synchronous workers assume that your application is resource-bound in terms of CPU and network band-
width. Generally this means that your application shouldn’t do anything that takes an undefined amount of time. An
example of something that takes an undefined amount of time is a request to the internet. At some point the external
network will fail in such a way that clients will pile up on your servers. So, in this sense, any web application which
makes outgoing requests to APIs will benefit from an asynchronous worker.
This resource bound assumption is why we require a buffering proxy in front of a default configuration Gunicorn. If
you exposed synchronous workers to the internet, a DOS attack would be trivial by creating a load that trickles data to
the servers. For the curious, Hey is an example of this type of load.
Some examples of behavior requiring asynchronous workers:
• Applications making long blocking calls (Ie, external web services)
• Serving requests directly to the internet
• Streaming requests and responses
• Long polling
• Web sockets
• Comet
2.9.3 How Many Workers?
DO NOT scale the number of workers to the number of clients you expect to have. Gunicorn should only need 4-12
worker processes to handle hundreds or thousands of requests per second.
Gunicorn relies on the operating system to provide all of the load balancing when handling requests. Generally we
recommend (2 x $num_cores) + 1 as the number of workers to start off with. While not overly scientific, the
formula is based on the assumption that for a given core, one worker will be reading or writing from the socket while
the other worker is processing a request.
Obviously, your particular hardware and application are going to affect the optimal number of workers. Our recom-
mendation is to start with the above guess and tune using TTIN and TTOU signals while the application is under
load.
Always remember, there is such a thing as too many workers. After a point your worker processes will start thrashing
system resources decreasing the throughput of the entire system.
2.9.4 How Many Threads?
Since Gunicorn 19, a threads option can be used to process requests in multiple threads. Using threads assumes use of
the gthread worker. One benefit from threads is that requests can take longer than the worker timeout while notifying
the master process that it is not frozen and should not be killed. Depending on the system, using multiple threads,
multiple worker processes, or some mixture, may yield the best results. For example, CPython may not perform as well
as Jython when using threads, as threading is implemented differently by each. Using threads instead of processes is a
good way to reduce the memory footprint of Gunicorn, while still allowing for application upgrades using the reload
40 Chapter 2. Contents