Python / Uvicorn Fundamentals Interview Questions
How do you deploy Uvicorn with Gunicorn for production?
For production deployments, the recommended pattern is to use Gunicorn as the process manager with Uvicorn as the worker class. Gunicorn provides mature process management (graceful restarts, signal handling, worker health monitoring) while Uvicorn provides the ASGI event loop inside each worker.
# Install the uvicorn-worker package (the uvicorn.workers module is deprecated) pip install uvicorn-worker # Run with Gunicorn + UvicornWorker gunicorn main:app -w 4 -k uvicorn_worker.UvicornWorker # Full production command: gunicorn main:app \ --workers 4 \ --worker-class uvicorn_worker.UvicornWorker \ --bind 0.0.0.0:8000 \ --timeout 120 \ --keep-alive 5 \ --access-logfile - \ --error-logfile - # PyPy-compatible configuration: gunicorn main:app -w 4 -k uvicorn_worker.UvicornH11Worker # Using a gunicorn.conf.py file # gunicorn --config gunicorn.conf.py main:app
# gunicorn.conf.py import multiprocessing bind = "0.0.0.0:8000" workers = (2 * multiprocessing.cpu_count()) + 1 worker_class = "uvicorn_worker.UvicornWorker" worker_connections = 1000 max_requests = 10000 max_requests_jitter = 1000 timeout = 120 graceful_timeout = 30 keepalive = 5
Note on the deprecated module: the uvicorn.workers module is deprecated and will be removed in a future release. The replacement is the standalone uvicorn-worker package (pip install uvicorn-worker). Update your -k flag from uvicorn.workers.UvicornWorker to uvicorn_worker.UvicornWorker.
More Related questions...