What is FastAPI's async support?
Answer
FastAPI is built on ASGI and fully supports Python's async/await for non-blocking I/O. Define async route handlers with async def: @app.get('/data') async def get_data(): result = await fetch_from_db(); return result. When you use async def, FastAPI runs the handler in the event loop directly — it can handle thousands of concurrent requests without threads. For CPU-bound work or code that cannot be made async (calling synchronous libraries), use def instead of async def — FastAPI runs sync handlers in a thread pool automatically, preventing the event loop from blocking. This is a key insight: sync handlers are fine in FastAPI but won't benefit from the async concurrency. Use async def when calling async-compatible libraries: httpx (not requests), asyncpg (not psycopg2), motor (async MongoDB).