FastAPI interview question · Most asked

What is ASGI and how does it differ from WSGI?

Summary

WSGI is the synchronous Python interface between servers and apps (one request per call). ASGI is its async successor, supporting async request handling, WebSockets, HTTP/2 and long-lived connections.

Longer Answer

A WSGI app is a callable app(environ, start_response) that blocks a worker thread per request. An ASGI app is async def app(scope, receive, send), so one event loop can multiplex thousands of concurrent connections. FastAPI/Starlette are ASGI apps served by Uvicorn, Hypercorn or Granian.

Key Points

  • WSGI: sync, request/response only
  • ASGI: async, WebSockets, lifespan events
  • Servers: Uvicorn, Hypercorn, Granian
  • ASGI apps can still run sync code in a threadpool

Deep dive: Request Lifecycle course: WSGI, ASGI and the Event Loop

More most-asked FastAPI questions

← All FastAPI questions