Quickstart Guide
Relevant source files
The following files were used as context for generating this wiki page:
Uvicorn is a lightning-fast ASGI (Asynchronous Server Gateway Interface) web server implementation for Python. It provides a minimal low-level interface between async web frameworks and servers, supporting HTTP/1.1 and WebSockets. Sources: README.md:1-25, mkdocs.yml:40-43
This guide provides the essential steps to install, configure, and run a basic application using Uvicorn. For more advanced configurations, see Settings.
Installation
Uvicorn can be installed using pip. There are two primary installation methods depending on whether you require a pure Python environment or high-performance Cython-based dependencies. Sources: README.md:29-45
Installation Options
| Package | Description | Key Components |
|---|---|---|
uvicorn | Minimal installation with pure Python dependencies. | Pure Python h11 for HTTP. |
uvicorn[standard] | Includes Cython-based extras for maximum performance. | uvloop, httptools, watchfiles, websockets. |
Sources: README.md:29-45, pyproject.toml:43-55
Creating a Basic Application
An ASGI application is a single asynchronous callable. It takes three arguments: scope, receive, and send. Sources: README.md:47-64
async def app(scope, receive, send):
assert scope['type'] == 'http'
await send({
'type': 'http.response.start',
'status': 200,
'headers': [
(b'content-type', b'text/plain'),
],
})
await send({
'type': 'http.response.body',
'body': b'Hello, world!',
})
Sources: README.md:47-64
Running the Server
Uvicorn can be executed via the command line or programmatically.
Command Line Interface (CLI)
To run the application saved in a file named example.py:
$ uvicorn example:app
Sources: README.md:66-70
Programmatic Usage
You can also start Uvicorn directly from within a Python script using the uvicorn.run() function. Sources: uvicorn/main.py:461-536
import uvicorn
if __name__ == "__main__":
uvicorn.run("example:app", host="127.0.0.1", port=8000, log_level="info")
Sources: uvicorn/main.py:461-536
Architecture and Data Flow
The following diagram illustrates the interaction between the Client, Uvicorn (the Server), and the ASGI Application during a standard HTTP request/response cycle.
The server parses raw data into a scope dictionary and uses receive/send channels to communicate with the app. Sources: uvicorn/protocols/http/httptools_impl.py:274-320, uvicorn/config.py:456-465
Key Configuration Options
Uvicorn offers extensive configuration through CLI flags or the Config class. Sources: uvicorn/config.py:228-305, uvicorn/main.py:65-310
| Option | Type | Default | Description |
|---|---|---|---|
host | str | "127.0.0.1" | Bind socket to this host. |
port | int | 8000 | Bind socket to this port. |
reload | bool | False | Enable auto-reload on code changes. |
workers | int | None | Number of worker processes. |
log_level | str | "info" | Logging level (debug, info, warning, error, critical). |
loop | str | "auto" | Event loop implementation (asyncio, uvloop). |
Sources: uvicorn/main.py:65-310, uvicorn/config.py:228-305
Summary
Uvicorn provides a high-performance bridge for running asynchronous Python web applications. By supporting the ASGI specification, it allows developers to build scalable applications that handle HTTP and WebSockets efficiently while maintaining compatibility across various async frameworks. Sources: README.md:1-25, uvicorn/main.py:538-545