tezvyn:

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

AI-drafted, machine-checkedSource: fastapi.tiangolo.combeginner
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 THIS TESTS: 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.

A GOOD ANSWER COVERS: 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.

COMMON WRONG ANSWERS: 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.

LIKELY FOLLOW-UPS: 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.

ONE 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.

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.