Performance: Async vs sync decision
HIGH IMPACT
This affects how quickly the server can respond to requests and handle multiple users at once.
from fastapi import FastAPI import asyncio app = FastAPI() @app.get('/async') async def read_async(): await asyncio.sleep(2) # non-blocking wait return {'message': 'done'}
from fastapi import FastAPI app = FastAPI() @app.get('/sync') def read_sync(): import time time.sleep(2) # blocking wait return {'message': 'done'}
| Pattern | Concurrency | Event Loop Blocking | Response Delay | Verdict |
|---|---|---|---|---|
| Synchronous endpoint | Limited by thread pool size | Yes (blocks threads during I/O) | High under load | [X] Bad |
| Asynchronous endpoint | Multiple requests concurrently | Non-blocking event loop | Low even under load | [OK] Good |