Skip to main content

Appendix-01-ASGIApplication

What ASGI Is

ASGI is the spiritual successor to WSGI (Web Server Gateway Interface).

  • WSGI was made for synchronous Python web apps (e.g. Flask, Django before Channels).
  • ASGI extends this idea to support asynchronous communication — like WebSockets, HTTP/2, and background tasks.

In simple terms:

ASGI defines how an async Python web app communicates with an async web server (like Uvicorn, Hypercorn, Daphne).

What an ASGIApplication Is

An ASGIApplication is just any callable object that follows the ASGI specification, i.e.:

async def app(scope, receive, send):
...
  • scope: Information about the connection (type = "http", "websocket", etc.)
  • receive: An awaitable that yields incoming events (like HTTP requests, WebSocket messages)
  • send: A callable used to send responses/events back to the client

So, a framework might define a class like:

class ASGIApplication:
async def __call__(self, scope, receive, send):
# handle the request
...

This makes it a valid ASGI app, because any ASGI server can call it.

Why Frameworks Use the Name ASGIApplication

Frameworks often call something ASGIApplication because:

  1. It’s the entry point that the ASGI server will invoke (similar to WSGIApplication in older frameworks).
  2. It clearly communicates compatibility — the framework or component is designed to be used in an ASGI environment.
  3. It’s flexible — multiple ASGI apps can be composed together (e.g. mounting FastAPI inside Starlette).

Example

Here’s a minimal custom ASGI application:

class SimpleASGIApp:
async def __call__(self, scope, receive, send):
if 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, ASGI world!',
})

You could then run this with:

uvicorn myapp:SimpleASGIApp

Summary

ConceptDescription
ASGIAsync Server Gateway Interface (modern, async version of WSGI)
ASGIApplicationA callable object conforming to ASGI spec (__call__(scope, receive, send))
Why the name?To indicate that the object is an ASGI-compatible application entry point