Python & FastAPI•Part 4: Web Frameworks, APIs & FastAPI (41 - 55)
WSGI vs ASGI:
In Simple Words:
- WSGI (Flask, Django): Synchronous request-response specification. One thread per request; cannot handle WebSockets or long-polling efficiently.
- ASGI (FastAPI, Starlette): Asynchronous server interface. Handles thousands of concurrent connections and WebSockets on a single thread.
python
# Raw ASGI application specification:
async def simple_asgi_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 from ASGI!'})