What is the difference between def and async def in Python and FastAPI?

Tests event-loop boundaries: async def yields control via await for non-blocking I/O, def runs in a threadpool. Use async def only with async libraries; def covers blocking calls. Red flag: claiming async is automatically faster or awaiting inside def.
What's really being asked
This question probes whether you understand the concurrency model of ASGI servers and the event loop. FastAPI runs on Starlette and Uvicorn, which use an event loop for async code. The interviewer wants to know that you grasp the difference between cooperative multitasking on the main loop and threadpool-based execution for blocking code, and that you can choose the right function type for a given I/O pattern.
The full answer
First, state that a regular def function is a standard synchronous function that blocks the thread until it completes, while an async def function returns a coroutine that yields control back to the event loop at each await point. Second, explain that in FastAPI a regular path operation function defined with def is run in a threadpool so it does not block the event loop, whereas an async def path operation runs directly on the event loop and must only contain non-blocking awaited calls. Third, give the decision rule: use async def when you are calling async libraries such as httpx, async SQLAlchemy, or aiobotocore; use def when you are using standard blocking libraries like the synchronous requests package, pandas, or time.sleep. Fourth, mention that mixing blocking code inside async def will freeze the entire server for all concurrent requests because the event loop cannot switch tasks.
The mistakes people make
A red flag is claiming that async def is always faster or that you should make every endpoint async by default. Another mistake is saying that def and async def are interchangeable in FastAPI; they are not, because await is invalid syntax inside a regular def function and blocking calls inside async def stall the loop. Some candidates also confuse parallelism with concurrency, suggesting that async creates new threads or processes automatically.
What usually comes next
The interviewer may ask what happens if you call time.sleep inside an async def endpoint, or how you would call a blocking library from an async endpoint if you had no alternative. They might also ask about the performance implications of running hundreds of def endpoints versus async ones, or how dependencies and sub-dependencies inherit the async or sync behavior in FastAPI.
A concrete example
Imagine an endpoint that queries a Postgres database. If you use asyncpg and async SQLAlchemy, define the endpoint with async def and await the query so the event loop can handle other requests while waiting for the database response. If you use psycopg2, define the endpoint with def so FastAPI runs it in a threadpool; the thread blocks, but the event loop remains free. Never await a psycopg2 query inside an async def endpoint because that blocks the main thread and destroys concurrency.
Interview question
You write an async def FastAPI endpoint that calls requests.get. What is the main risk?
- a.FastAPI automatically moves the blocking call into a threadpool so it remains safe.
- b.It runs correctly but is only slightly slower than using def for the same blocking call.
- c.The event loop is blocked, freezing concurrent request handling until the call finishes.Correct
- d.Python raises a SyntaxError because blocking libraries cannot be used inside async def.
Why? this is the answer
The correct answer is C because calling a blocking library like requests inside async def stalls the event loop, stopping all other requests. The most tempting distractor is A because beginners often assume FastAPI magically threadpools any blocking code, but only def endpoints are run in a threadpool.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
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 python — each one lists the topics its interview covers.
See open roles