Gunicorn and Uvicorn
Gunicorn is the most mature WSGI server in the Python ecosystem, and Uvicorn is an ASGI server based on an asynchronous event loop. The two are often used together: Gunicorn handles multi-process management and graceful restarts, while Uvicorn provides asynchronous request handling capability, together forming the standard entry point for Python web services in production.
Core Principles
To understand this combination, first distinguish the two application protocols WSGI and ASGI. WSGI is a synchronous, request-and-response style interface: one request occupies one work unit until processing completes and the response is returned. ASGI, by contrast, supports async coroutines, allowing control to be yielded while waiting for I/O, so a single process can handle hundreds or thousands of connections at once, and it natively supports long connections such as WebSocket and Server-Sent Events.
Gunicorn itself uses a pre-fork model: after the master process starts, it forks several worker processes according to configuration, and the master does not handle requests but only listens, forwards, detects dead processes, and restarts them. Its value lies in stable process management, signal control, logging, and graceful shutdown. Uvicorn was originally a standalone event loop server and later became widely used as a worker class for Gunicorn, connecting ASGI applications to Gunicorn's process system through uvicorn.workers.UvicornWorker.
The request path is: a client connection is accepted by the master process's listening socket, dispatched to a worker process through a shared file descriptor, and inside the worker handed to the event loop to schedule coroutines, ultimately calling the application's callable object and writing back the response. Uvicorn is internally based on high-performance implementations such as uvloop and httptools, keeping protocol parsing and event loop overhead very low, while Gunicorn's sync worker handles only one request per process at a time, simple to implement and predictable in behavior. The two are not substitutes but a division of labor: one handles the process lifecycle, the other connections and concurrency.
Key Points
One, selection judgment. For pure synchronous frameworks such as Flask and traditional Django views, gunicorn app:app with the default sync worker is enough; for asynchronous frameworks such as FastAPI and Starlette, an ASGI worker must be used, a common form being gunicorn app.main:app -k uvicorn.workers.UvicornWorker. If your service only needs a small amount of concurrency on a single machine, running uvicorn --workers N directly also works, but it lacks Gunicorn's process supervision capability.
Two, worker count and CPU cores. The classic rule of thumb is twice the core count plus one, that is, (2 x CPU) + 1. The reason is that synchronous workers often have blocking waits, and configuring more of them can improve CPU utilization. But for asynchronous workers this formula no longer holds: coroutines within one process can already handle very high concurrency, and too many workers only bring doubled memory and context-switching overhead. Asynchronous services are usually configured by CPU core count, or even core count plus one, with the rest handled by concurrent connections.
Three, timeouts and warmup. --timeout controls how long a worker must be silent before it is considered dead, thirty seconds by default; interfaces handling large files or long tasks need it explicitly enlarged, otherwise they will be killed by the master process. Use --graceful-timeout to ensure old processes can finish in-flight requests during rolling restarts. --max-requests and jitter parameters should also be set so workers are recycled periodically, mitigating potential memory leaks.
Four, reverse proxy position. Gunicorn should not be exposed directly to the public internet but placed behind Nginx, with Nginx handling TLS, static files, compression, and rate limiting. In this case --forwarded-allow-ips must be enabled so the application correctly identifies the client's real IP and protocol.
Common Misconceptions
First, putting synchronous code that needs long blocking into an asynchronous worker: one request stalls the entire event loop, manifesting as all interfaces slowing down at once; blocking tasks should be handed to a thread pool or a separate queue, or simply split into a separate synchronous service. Second, mistakenly believing more workers means higher throughput and ignoring each process's independent memory usage, which is especially fatal for model-loading services, frequently killed by the system under container memory limits, with a restart loop more harmful than slowness. Third, forgetting that synchronous frameworks lack adaptation when paired with asynchronous workers, causing errors at startup or abnormal behavior; the correct approach is to confirm the framework version supports an ASGI entry point before switching the worker class.
There is also a class of hidden problems at the load balancing layer: multiple workers do not share sessions or in-memory caches, so if the application stores state in-process, the same user's requests landing on different processes will cause the state to disappear.
Application Scenarios
FastAPI API services, Django asynchronous views, model inference APIs, WebSocket real-time push, internal microservice gateways, and containerized Python backends.