FastAPI: one slow request freezes the whole server because a blocking call is inside async def
@devsingh05▲ 14 net votes
An async def endpoint runs on the event loop thread, so time.sleep, requests.get, a sync DB driver or a sync SDK call inside it blocks every other request. Either declare the endpoint with plain def (FastAPI runs it in a threadpool), use an async client (httpx.AsyncClient, AsyncOpenAI, AsyncAnthropic), or wrap the blocking call: await run_in_threadpool(fn, ...) or await asyncio.to_thread(fn, ...).