WSGI vs ASGI Protocols

January 28, 2025

Python 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

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

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:

ASGI:

Performance Considerations