tezvyn:

Async Path Operations in FastAPI

AI-drafted, machine-checkedSource: fastapi.tiangolo.comadvanced
Async Path Operations in FastAPI

FastAPI path operations can be async, letting the server switch to other requests during I/O waits. Declare dependencies and sub-dependencies async when they await external calls.

WHY IT EXISTS: Web servers spend most of their time waiting on networks and disks. If a path operation function blocked the entire thread during every database query or external call, the server would sit idle and throughput would collapse. Async path operations solve this by letting a single process suspend a waiting task and handle other requests, extracting far more concurrency from the same hardware.

THE MENTAL MODEL: Think of the burger analogy from the docs. Concurrent burgers mean one cook flips between orders while patties are on the grill. Parallel burgers mean two cooks on two grills. An async path operation is the single cook. While a database query grills, the cook serves another customer. You are not adding more CPU; you are eliminating idle waiting time.

HOW IT WORKS: You declare a path operation function with async def. Inside it you use await to pause for coroutines. FastAPI runs this inside an event loop. The documentation notes that dependencies and sub-dependencies can also be async, so the entire request chain can suspend and resume together. If you use a regular def path operation, FastAPI runs it in a thread pool to protect the loop, but that incurs thread-switching overhead.

WHEN TO USE IT: Use async def for path operation functions that wait on external I/O. This includes database queries, HTTP calls to other services, file system reads, or background task handoffs. Use async dependencies and sub-dependencies when those layers also perform waits. The payoff appears under load when many simultaneous requests are parked awaiting responses.

WHEN NOT TO USE IT: Do not use async def for CPU-intensive work inside the path operation. Heavy computation does not yield control to the loop; it starves other requests. Do not drop synchronous I/O calls into an async def route because they block the event loop exactly like CPU work, freezing concurrency for every client on that worker.

ONE CANONICAL EXAMPLE: A route that fetches data from a database. The path operation function is declared async def. It awaits the database driver. A dependency that opens the session is also declared async def. While the database server processes the query, the event loop picks up the next request. If the developer instead placed a blocking database call inside that async route, the worker would freeze for all other requests during every wait.

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.