Performance: Why async improves performance
Async improves server responsiveness and throughput by allowing concurrent handling of requests without blocking, which speeds up response time and reduces waiting.
from fastapi import FastAPI import asyncio app = FastAPI() @app.get("/async") async def async_endpoint(): await asyncio.sleep(2) # non-blocking wait return {"message": "done"}
from fastapi import FastAPI import time app = FastAPI() @app.get("/sync") def sync_endpoint(): time.sleep(2) # blocking wait return {"message": "done"}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous blocking endpoint | N/A (server-side) | N/A | N/A | [X] Bad |
| Asynchronous non-blocking endpoint | N/A (server-side) | N/A | N/A | [OK] Good |
async functions in FastAPI improve performance?async def before the function name.async def to allow await inside the function.from fastapi import FastAPI
app = FastAPI()
@app.get('/items')
def read_items():
data = await fetch_data()
return dataPass the test and this pattern moves unknown