Uvicorn is a lightweight ASGI web server for Python that accepts HTTP and WebSocket connections and passes them to an asynchronous application through the Asynchronous Server Gateway Interface. Among web servers, it occupies the application layer: it is not a general-purpose server for static files or virtual hosts, but the process that actually executes Python code for frameworks such as FastAPI, Starlette and Django in its asynchronous mode.
Uvicorn is built around a single event loop per process, which lets one worker hold thousands of open connections while waiting on databases, external APIs or slow clients. In production it is normally placed behind a reverse proxy such as Nginx, Caddy or Traefik and run with several worker processes, either through its own process manager or under Gunicorn.
How Uvicorn serves an ASGI application
Uvicorn translates network traffic into ASGI events and hands them to an application callable that receives three arguments: a scope dictionary describing the connection, a receive coroutine and a send coroutine. The application never touches sockets directly; it awaits incoming messages and sends response events back, while Uvicorn handles parsing, flow control and connection lifetime.
Internally, Uvicorn runs on asyncio, optionally replacing the default loop with uvloop, a faster implementation built on libuv. HTTP/1.1 parsing uses either the httptools library, a binding to the Node.js parser, or the pure-Python h11 library. WebSocket support comes from the websockets or wsproto packages. Installing uvicorn[standard] pulls in the faster optional components where the platform supports them.
The following table lists the main components of a Uvicorn process and the role each one plays.
| Component | Default choice | Faster alternative | Responsibility |
|---|---|---|---|
| Event loop | asyncio | uvloop | Schedules coroutines and socket I/O |
| HTTP parser | h11 | httptools | Turns bytes into requests and headers |
| WebSocket protocol | websockets | wsproto (alternative) | Handles upgrade and frames |
| Lifespan handler | auto | on or off | Runs startup and shutdown hooks |
| Process manager | Single process | –workers or Gunicorn | Uses several CPU cores |
Uvicorn compared with other Python servers
Uvicorn differs from classic Python servers mainly in the interface it speaks. Gunicorn and uWSGI were designed for WSGI, a synchronous standard in which each request occupies a worker thread or process until it finishes. ASGI adds native support for long-lived connections, streaming and WebSockets, which WSGI cannot express cleanly.
The comparison below places Uvicorn next to the servers it is most often evaluated against.
| Server | Interface | Concurrency model | Typical role |
|---|---|---|---|
| Uvicorn | ASGI | Async event loop per worker | FastAPI and Starlette services |
| Gunicorn | WSGI (ASGI via workers) | Pre-fork processes | Process manager and Flask hosting |
| uWSGI | WSGI, partial ASGI | Processes and threads | Legacy Django and Flask stacks |
| Hypercorn | ASGI | asyncio or Trio | HTTP/2 and HTTP/3 needs |
| Daphne | ASGI | Twisted reactor | Django Channels deployments |
A key limitation is protocol coverage: Uvicorn implements HTTP/1.1 and WebSockets but does not serve HTTP/2 on its own. When HTTP/2 or HTTP/3 matters, the usual answer is to terminate it at the reverse proxy and speak HTTP/1.1 to Uvicorn over a local socket, or to choose Hypercorn.
Running Uvicorn in production
A production Uvicorn deployment combines several worker processes, a supervising process and a front-end proxy. The command uvicorn app.main:app --host 127.0.0.1 --port 8000 --workers 4 starts four independent processes, each with its own event loop. A common starting point is one worker per CPU core, adjusted after load testing, because async workers rarely benefit from heavy oversubscription.
Many teams instead run Gunicorn with the uvicorn.workers.UvicornWorker class, gaining mature process supervision, graceful reloads and worker recycling. Newer Uvicorn releases include their own supervisor with similar restart behavior, and the separate uvicorn-worker package now carries the Gunicorn worker class. In containers, a frequent pattern is one Uvicorn process per container, letting Kubernetes or another orchestrator handle replication.
The table below summarizes the settings that most affect behavior under real traffic.
| Option | Typical value | Effect |
|---|---|---|
--workers | 1 per CPU core | Parallelism across cores |
--proxy-headers | Enabled behind a proxy | Trusts X-Forwarded-For and protocol |
--forwarded-allow-ips | Proxy address only | Limits which hosts may set headers |
--timeout-keep-alive | 5 seconds (default) | Closes idle keep-alive connections |
--limit-concurrency | Set from load tests | Returns 503 above the limit |
--uds | Unix socket path | Avoids TCP overhead to a local proxy |
--reload | Development only | Restarts on code changes |
The reverse proxy in front of Uvicorn should handle TLS termination, static files, request buffering and protection against slow clients. Uvicorn can serve TLS directly with certificate options, but delegating it to Nginx or Caddy keeps certificate management and connection hardening in one place.
Performance characteristics and trade-offs
Uvicorn performs best on I/O-bound workloads, where request handlers spend most of their time awaiting network or disk operations. One worker can multiplex many such requests because each await yields control back to the loop. Throughput figures in benchmarks vary widely with hardware, payload and framework, so published numbers rarely transfer to a real application without measurement.
The model has a sharp edge: blocking code stalls the whole worker. A synchronous database driver, a CPU-heavy loop or a call to time.sleep inside an async def handler freezes every connection on that event loop. Frameworks mitigate this by running plain def endpoints in a thread pool, but heavy computation still belongs in separate processes or task queues.
The following table connects common symptoms with their usual causes and fixes.
| Symptom | Likely cause | Remedy |
|---|---|---|
| Latency spikes for all users | Blocking call in async handler | Use async drivers or a thread pool |
| Client IP always shows the proxy | Proxy headers not trusted | Enable proxy headers for the proxy IP |
| Wrong scheme in redirects | Missing forwarded protocol | Set X-Forwarded-Proto at the proxy |
| Memory grows over days | Leak in application code | Recycle workers and profile memory |
| Startup code runs repeatedly | Several workers each run lifespan | Move one-time jobs out of startup |
Common mistakes when deploying Uvicorn
- Leaving –reload enabled in production, which adds file watching overhead and unexpected restarts.
- Binding to
0.0.0.0on a public interface without a reverse proxy, exposing the server to slow-client and header abuse. - Trusting forwarded headers from any source, which lets clients spoof their IP address.
- Assuming more workers always help, when database connection limits are often the real bottleneck.
- Storing session state in process memory, which breaks as soon as requests reach different workers.
Put simply
Uvicorn is the part that sits between the internet and a Python program. When a browser or an app sends a request, Uvicorn receives it, hands it to the program, and sends the answer back. It is especially good at juggling many conversations at once, the way a skilled waiter takes several orders while the kitchen cooks instead of standing beside one table until the food arrives.
It is usually not alone. A front-door server such as Nginx greets visitors, handles security certificates and passes the real work to Uvicorn. The key point is simple: Uvicorn runs modern asynchronous Python web applications, and it works best when nothing inside the program makes everyone wait.
In practice
A logistics company builds a FastAPI service that returns live parcel locations to a mobile app and pushes status updates over WebSockets. The service runs on a virtual machine with 4 CPU cores. The team starts Uvicorn with four workers on a Unix socket, places Nginx in front for TLS and compression, and enables proxy headers restricted to the local address so that logs show real client IPs.
During a holiday peak, monitoring shows latency climbing even though CPU usage stays near 30 percent. Profiling reveals a synchronous HTTP client used to call a carrier API inside an async endpoint. After switching to an asynchronous client and capping database connections per worker, the same machine holds about 6,000 concurrent WebSocket connections with median response times back under 80 milliseconds, and no extra hardware is needed.