Python / Uvicorn Fundamentals Interview Questions
What are Uvicorn workers and how do you configure multi-process deployments?
By default Uvicorn runs as a single process with a single event loop. To utilise multiple CPU cores and handle more concurrent requests, you can run multiple worker processes using the --workers flag.
# Run 4 worker processes uvicorn main:app --workers 4 # Workers can also be set via environment variable: export WEB_CONCURRENCY=4 uvicorn main:app # Common formula for worker count: # workers = (2 x CPU cores) + 1 for I/O-bound apps # workers = CPU cores for CPU-bound apps import multiprocessing workers = (2 * multiprocessing.cpu_count()) + 1 # IMPORTANT: --workers and --reload are mutually exclusive
How multi-worker mode works: Uvicorn spawns the specified number of separate OS processes, each running its own event loop and handling requests independently. There is no shared state between workers - each process is completely independent. This is true parallelism (bypasses the GIL) unlike threading.
| Aspect | Single worker (default) | Multiple workers |
|---|---|---|
| CPU utilisation | One core | Multiple cores (true parallelism) |
| Shared state | N/A | Not shared - each worker is isolated |
| Process management | Simple | Use Gunicorn or systemd for production |
| Auto-reload | Supported | Not compatible with --reload |
| Memory | Lower | Each worker has its own memory space |
Production recommendation: for production multi-worker deployments, use Gunicorn with UvicornWorker rather than Uvicorn's built-in --workers. Gunicorn provides superior process lifecycle management, graceful restarts, and health monitoring.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
