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.
Interview question
A developer implements a CPU-intensive image resize inside an async def FastAPI endpoint. Under concurrent load, what is the most likely outcome?
- a.Throughput improves because async def enables true parallel execution across CPU cores
- b.The event loop blocks during each resize, starving other concurrent requestsCorrect
- c.FastAPI automatically moves the CPU work to a thread pool to protect the event loop
- d.The worker handles other requests while each image resize operation runs
Why? this is the answer
CPU-intensive work inside async def never yields control to the event loop, so it starves other requests on the same worker. FastAPI only runs regular def routes in a thread pool, and async does not create parallel multicore execution.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
- #fastapi
- #async
- #concurrency
- #python
- #path-operations
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.
See open roles