Senior LevelPython
How does the ASGI (Asynchronous Server Gateway Interface) specification differ from legacy WSGI, and how does FastAPI/Starlette process asynchronous requests?
❓ Interview Prompt: Compare WSGI (gunicorn/uWSGI, synchronous thread blocking per request) vs ASGI (uvicorn/hypercorn, event-driven async callables `(scope, receive, send)`).
1. Legacy WSGI (Web Server Gateway Interface):
•
Synchronous specification ('def application(environ, start_response)').
•
Requires 1 OS thread per active HTTP connection, bottlenecking under high concurrent WebSocket or SSE connections.
2. Modern ASGI (Asynchronous Server Gateway Interface):
•
Asynchronous event-driven spec ('async def app(scope, receive, send)').
•
Powers FastAPI / Starlette over Uvicorn, handling 100,000+ concurrent async HTTP/2 and WebSocket streams on a single thread!
Code Implementation Example
python
# Raw ASGI Application Interface
async def app(scope, receive, send):
assert 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!',
})💡Implementing low-level ASGI async application interface.
🎯 Interview Key Takeaway
ASGI enables event-driven asynchronous web frameworks like FastAPI to handle thousands of concurrent WebSocket and HTTP connections.
#ASGI#WSGI#FastAPI#Starlette#Senior
Back to all Python Questions→