ASGI-and-WSGI
ASGI vs WSGI: What's the Difference?
WSGI (Web Server Gateway Interface)
- WSGI is the standard interface between Python web applications and web servers.
- It is synchronous: each request is handled one at a time, blocking until finished.
- Used by frameworks like Django (pre-3.0), Flask, Pyramid, etc.
- Good for traditional web apps and APIs that don't need real-time features.
How it works: The server calls your app for each request, waits for the response, then moves to the next request.
ASGI (Asynchronous Server Gateway Interface)
- ASGI is the newer standard designed to support asynchronous Python web apps.
- Supports both synchronous and asynchronous code.
- Enables features like WebSockets, long-lived connections, background tasks, and high concurrency.
- Used by frameworks like FastAPI, Starlette, Django 3.0+, etc.
How it works: The server can handle many requests at once, using async/await, and can also handle protocols like WebSockets (for real-time communication).
Key Differences Table
Feature | WSGI | ASGI |
---|---|---|
Concurrency | Synchronous (one at a time) | Asynchronous (many at once) |
Protocols | HTTP only | HTTP, WebSocket, more |
Use Cases | Traditional web apps/APIs | Real-time apps, async APIs, WebSocket |
Frameworks | Flask, Django (<3.0 ), etc. | FastAPI, Starlette, Django (3.0+ ), etc. |
Analogy
- WSGI is like a single-lane road: one car (request) at a time.
- ASGI is like a multi-lane highway: many cars (requests) can move at once, and some can even take different routes (protocols).
Summary:
- Use WSGI for simple, traditional web apps.
- Use ASGI for modern, high-concurrency, or real-time apps (like with FastAPI).