Sub topic 8
WSGI and ASGI: How Python Apps Talk to Servers
This chapter assumes that you understand HTTP requests and responses, can use a Linux shell, and have basic Python knowledge. It also assumes that you can create a virtual environment and install packages with pip.
The application server boundary
A Python web application usually does not listen directly for HTTP connections. A web server or application server receives the network request, converts it into the interface expected by the application, and converts the application's result back into an HTTP response.
WSGI, the Web Server Gateway Interface, is the traditional interface for synchronous Python web applications. Synchronous code handles one operation at a time in each worker. Frameworks such as Flask and older Django applications commonly use WSGI.
A WSGI application is a callable. It receives a request environment and a function used to start the response. It returns an iterable containing byte strings.
def application(environ, start_response):
body = b"Hello from WSGI\n"
start_response("200 OK", [
("Content-Type", "text/plain"),
("Content-Length", str(len(body))),
])
return [body]Save this as app.py. A WSGI server can load it by referring to app:application, where the first part is the module name and the second part is the callable name.
ASGI for asynchronous applications
ASGI, the Asynchronous Server Gateway Interface, extends this model for asynchronous Python. It supports normal HTTP requests, WebSockets, and long-lived connections. An asynchronous application can pause while waiting for a database, network service, or other operation, allowing the event loop to handle other work.
An ASGI application receives three values: the connection scope, a function that receives events, and a function that sends events. The application is asynchronous and must be awaited.
async def application(scope, receive, send):
if scope["type"] != "http":
return
body = b"Hello from ASGI\n"
await send({
"type": "http.response.start",
"status": 200,
"headers": [
[b"content-type", b"text/plain"],
[b"content-length", str(len(body)).encode()],
],
})
await send({
"type": "http.response.body",
"body": body,
})Run an ASGI application with a server such as Uvicorn. A WSGI application needs a WSGI-compatible server such as Gunicorn.
| Interface | Main execution style | Common use |
|---|---|---|
| WSGI | Synchronous request handling | Flask and traditional Django |
| ASGI | Synchronous and asynchronous handling | WebSockets and async frameworks |
Choosing between WSGI and ASGI
Use WSGI when the application and its libraries are synchronous. It is simple, widely supported, and often sufficient for ordinary web pages and APIs.
Use ASGI when the application needs asynchronous endpoints, WebSockets, streaming, or many connections that spend time waiting. ASGI does not automatically make blocking code asynchronous. A blocking database driver can still block an event loop, so use async-compatible libraries or move blocking work to a suitable worker.
Exercise
Which interface is the better fit for an application that provides WebSockets and uses asynchronous database calls?
Show answer
ASGI, because it supports WebSockets and asynchronous request handling.