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.
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...
