liveBook · Manning

ASGI

As explained in Python Concurrency with asyncio

ASGI is the Asynchronous Server Gateway Interface standard for asynchronous communication between Python web servers, frameworks, and applications.

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

How Python Concurrency with asyncio covers it

How deep it goes
Defines the coroutine interface and contrasts WSGI, but defers deeper protocol evolution and implementation details.
Examples
Demonstrates a minimal response, Starlette REST and WebSocket endpoints, and Django asynchronous views.
What it assumes
Assumes Python web application familiarity while introducing the asynchronous server interface from its contrast with WSGI.
What you can do after
Build ASGI applications and serve them with Uvicorn, optionally using Gunicorn workers.
How it connects
Connects asynchronous programming, WSGI, coroutines, WebSockets, Uvicorn, Starlette, and Django.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.