WSGI vs ASGI Protocols
January 28, 2025Python web applications require protocols to communicate between web servers and application code. WSGI (Web Server Gateway Interface) and ASGI (Asynchronous Server Gateway Interface) are two such protocols, each with distinct characteristics and use cases.
WSGI: Synchronous Web Applications
WSGI is the traditional protocol for Python web applications, introduced in PEP 333. It’s synchronous and handles one request at a time.
Core Concepts
- Single callable function (application object)
- Synchronous request-response cycle
- Compatible with most traditional web frameworks
Example WSGI Application
# wsgi_app.py
def simple_wsgi_app(environ, start_response):
status = '200 OK'
headers = [('Content-type', 'text/plain')]
start_response(status, headers)
return [b"Hello from WSGI!"]
gunicorn --workers=2 --bind 127.0.0.1:8000 wsgi_app:simple_wsgi_app
ASGI: Modern Async Applications
ASGI extends WSGI to support asynchronous operations, WebSockets, and HTTP/2, making it suitable for modern web applications.
Key Features
- Async-first design
- WebSocket support
- HTTP/2 compatibility
- Better handling of long-lived connections
Example ASGI Application
async def simple_asgi_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 from ASGI!",
})
uvicorn asgi_app:simple_asgi_app
When to Use Each
WSGI:
- Simple request-response applications
- Legacy systems integration
- CPU-bound applications
ASGI:
- Real-time applications
- WebSocket requirements
- High-concurrency needs
- I/O-bound applications
Performance Considerations
- ASGI handles concurrent connections more efficiently
- WSGI is simpler and may be faster for basic request-response cycles
- ASGI servers (like Uvicorn) generally show better performance under high load