David Y.
—How many Uvicorn workers should I use for a production deployment of a Python web service? How does the number of workers relate to the number of CPU cores I have, and what are the benefits of running additional workers?
The recommended baseline number of Uvicorn workers is one per thread plus one. Modern CPUs generally use hyperthreading and each CPU core will have two threads. Therefore, a 4-core server should be running 9 workers, an 8-core 17 workers, and a 16-core 33 workers, according to the formula below:
number of workers = number of cores * number of threads + 1
This gives each core one worker to read and write to and from the network socket and another to process requests. Make sure not to confuse the physical number of CPU cores with the system’s vCPUs (i.e. its threads) or you may start up too many.
However, the optimal number of workers will depend on the nature of the service and the hardware it’s running on, as well as how it is used. A good way to figure out the optimal number of workers for a particular service is to increase and decrease the number of workers while it’s operating under load, using the TTIN and TTOU signals and noting the points at which performance is enhanced and degraded.
Starting up too many workers can cause the server to run out of resources, which will slow down all operations.
Note that this guidance applies to both the asynchronous Uvicorn and the synchronous Gunicorn.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.