In FastAPI you must call a slow, blocking SDK. Which option avoids blocking the event loop?
Options
- Call it directly inside an
async defendpoint, as usual - Use a plain
defendpoint, or wrap it withasyncio.to_thread(Correct option) - Add
awaitin front of the synchronous call to the SDK - 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.