ASGI, the Asynchronous Server Gateway Interface, standardizes communication between asynchronous Python web servers, frameworks, and applications. It replaces WSGI for asyncio-based workloads and supports applications that need concurrency or long-lived connections. section 9.0 section 9.2
WSGI centers on a synchronous function and supports request/response lifecycles, leaving no place for await or WebSockets. ASGI redesigns the interface around coroutines and asynchronous message exchange. section 9.2.1
An ASGI application receives three arguments:
- scope: a dictionary describing the connection or request. section 9.2.1
- receive: a coroutine for receiving data. section 9.2.1
- send: a coroutine for sending data, such as an HTTP response start and body. section 9.2.1
Minimal ASGI response
A minimal application awaits send twice: first with an HTTP response-start message containing status and headers, then with an HTTP response-body message containing the response bytes.
Uvicorn can serve an ASGI application directly; combining it with Gunicorn provides worker-restart logic. Starlette supplies an ASGI-compliant framework for REST APIs and WebSockets, while Django supports ASGI deployment and asynchronous views. section 9.2.1 section 9.3 section 9.4
