An introduction to ASGI, Asynchronous Server Gateway Interface

Philip Jones

Abstract

WSGI has been a huge success, allowing for an ecosystem of servers and frameworks to exist and develop independently, however, WSGI is unable to support websockets or the new async/await syntax. ASGI is the asynchronous spiritual successor to WSGI, with websockets and async support built in.

Description

If you develop web appliations in Python you will almost certainly be doing so using WSGI, with the most popular frameworks, Django and Flask, as well as the majority of others being based upon it. WSGI specifies the interface between servers and applications, simplified this interface is,

def application(environ, start_response):
    start_response("200 OK", [("Content-Type", "text/plain")])
    return b"Hello, World"

with the application called on each request. The request description and environment is specified in the environ dict and the start_response is called to send the response status code and headers before the body, which is returned by the application.

ASGI also aims to specify the interface between servers and applications, only using the async/await syntax, the interface can be simplified to

async def application(scope, receive, send):
    await send({"type": "http.response.start", "status": 200, "headers": [(b"Content-Type", "text/plain")]})
    await send({"type": "http.response.body", "body": b"Hello World"})

with the application awaited on each request and scope fulfilling a similar role to environ.

In this talk I'll explain the above and how it limits WSGI and why ASGI is the solution. I'll also introduce the current ASGI ecosystem and features.

Outline

  • Introduction to WSGI (basic code)
  • WSGI ecosystem servers (Gunicorn, uWSGI, mod_wsgi) and frameworks (Flask, Django)
  • WSGI limitations e.g. Websockets
  • ASGI Introduction (basic code)
  • ASGI development and history
  • ASGI features, websockets, HTTP/2
  • ASGI ecosystem servers (Hypercorn, Daphne, Uvicorn) and frameworks (Starlette, Django, Quart).

Bio

I am the head of engineering at Octopus Wealth. I authored the Quart and Hypercorn frameworks and help maintain a number of sans-io libraries in the python-hyper organisation. Previously I was a particle physicist working underground in Canada.