Repaso
Español
Backend & APIsAdvancedAPI-16

In FastAPI you must call a slow, blocking SDK. Which option avoids blocking the event loop?

Options

  1. Call it directly inside an async def endpoint, as usual
  2. Use a plain def endpoint, or wrap it with asyncio.to_thread (Correct option)
  3. Add await in front of the synchronous call to the SDK
  4. Increase the timeout of the server so that the call can finish

Answer

Declare the endpoint with plain def (runs in a threadpool) or wrap the call with run_in_threadpool/asyncio.to_thread

Why

async def endpoints run directly on the event loop, which must never be blocked.

FastAPI runs plain def endpoints in a pool of worker threads, so a blocking call only blocks that thread.

Inside async code, await asyncio.to_thread(sdk.call) moves the blocking call to a thread. You cannot await a normal (non-async) function.

Remember

Blocking library → plain def endpoint or to_thread.

Key terms

Practice this question in Repaso