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.
More Related questions...