ASGI Ecosystem & Alternatives
Relevant source files
The following files were used as context for generating this wiki page:
The ASGI (Asynchronous Server Gateway Interface) specification fills the gap for a minimal, low-level server/application interface for Python asynchronous frameworks. Uvicorn serves as a lightning-fast implementation of this specification, supporting HTTP/1.1 and WebSockets. By decoupling the server implementation from the application framework, ASGI enables a rich ecosystem of interoperating web servers and frameworks.
Sources: README.md:16-21, README.md:89-92
Alternative ASGI Servers
Because ASGI is a standardized protocol, applications built for Uvicorn can typically run on other ASGI-compliant servers. This interoperability allows developers to choose servers based on specific feature requirements like HTTP/2 support or different concurrency backends.
Key Alternatives
| Server | Features & Description |
|---|---|
| Daphne | The first ASGI server, developed for Django Channels. Supports HTTP/1.1, HTTP/2, and WebSockets. |
| Hypercorn | Originally part of Quart. Supports HTTP/1.1, HTTP/2, WebSockets, and the trio async framework. |
| Granian | A Rust-based HTTP server compatible with ASGI, supporting HTTP/2, TLS, and WebSockets. |
| Mangum | An adapter specifically designed for running ASGI applications on AWS Lambda and API Gateway. |
Sources: README.md:94-123
Server Interoperability Flow
The following diagram illustrates how the ASGI specification allows different server implementations to interface with the same application logic.
The diagram shows that multiple server implementations (Uvicorn, Daphne, Hypercorn) can drive various frameworks by adhering to the common ASGI specification.
Sources: README.md:89-123
WSGI Compatibility & Transitions
While Uvicorn is built for the async ASGI world, it maintains a relationship with the legacy WSGI (Web Server Gateway Interface) ecosystem through middleware and specialized protocols.
WSGI Middleware
Uvicorn includes a WSGIMiddleware (often utilizing a2wsgi) that allows synchronous WSGI applications to run within the asynchronous ASGI environment. This is useful for legacy migration or using synchronous libraries within an async stack.
Sources: uvicorn/middleware/wsgi.py:126-130, uvicorn/config.py:488-490
Native WSGI Implementation
The project maintains a internal WSGI responder logic, although it is currently deprecated in favor of external specialized packages like a2wsgi.
class WSGIResponder:
def __init__(
self,
app: WSGIApp,
executor: concurrent.futures.ThreadPoolExecutor,
scope: HTTPScope,
):
self.app = app
self.executor = executor
self.scope = scope
# ... initialization of event and queue
Sources: uvicorn/middleware/wsgi.py:73-84
Implementation Varieties
Uvicorn itself offers "alternatives" in how it handles protocols, allowing users to choose between pure-Python implementations and high-performance C-based extensions.
Protocol Selection logic
Uvicorn can automatically select the best available implementation based on the environment (e.g., using uvloop on Linux but asyncio on Windows).
| Protocol Type | Auto-selection Mapping |
|---|---|
| HTTP | httptools (if installed) or h11 |
| WebSocket | websockets or wsproto |
| Loop | uvloop (if installed and supported) or asyncio |
Sources: uvicorn/config.py:34-60, README.md:28-44
Data Flow for WSGI Adaptation
When running a WSGI app through Uvicorn, the server must translate the asynchronous ASGI Scope and messages into a synchronous Environ dictionary.
The diagram outlines how Uvicorn uses a thread pool to bridge the gap between async request handling and synchronous WSGI execution.
Sources: uvicorn/middleware/wsgi.py:86-107
Ecosystem Dependencies
The "standard" installation of Uvicorn pulls in a specific set of tools that define its preferred ecosystem for production environments.
- uvloop: A fast, drop-in replacement for the built-in asyncio event loop.
- httptools: A Python binding for the nodejs HTTP parser.
- websockets: A library for building WebSocket servers and clients in Python.
- watchfiles: Used for the
--reloadfunctionality during development.
Sources: pyproject.toml:39-47, README.md:32-44
Summary
The ASGI ecosystem is designed for modularity, allowing Uvicorn to coexist with other servers like Daphne and Hypercorn. Uvicorn provides internal bridges to the WSGI ecosystem via middleware, though it encourages the use of modern async-native frameworks. Through its configurable protocol implementations, Uvicorn allows developers to balance between broad compatibility (pure-Python) and maximum performance (Cython-based extensions).
Sources: README.md:89-123, uvicorn/config.py:34-60