Python web frameworks like Django and Flask handle multiple simultaneous requests through concurrency instead of parallelism.
This means they use a single thread and asynchronous I/O to juggle multiple requests, rather than multiple threads/processes. Here's how it works under the hood:
import asyncio
async def handle_request(request):
response = # do something with request
return response
async def main():
await asyncio.gather(
handle_request(req1),
handle_request(req2)
)
The
The advantage over multi-threading is avoiding lock contention and simplified code. The tradeoff is CPU-bound tasks still execute sequentially. But most web workloads tend to be I/O heavy anyway.