Repaso
Español
PythonIntermediatePY-14

Inside an async def FastAPI endpoint you call time.sleep(2) (or requests.get). What happens under load?

Options

  1. Only that request waits 2 seconds; other requests keep running
  2. The event loop is blocked, so every other request on that worker stalls (Correct option)
  3. FastAPI moves the blocking call to a separate thread automatically
  4. Python raises an error because blocking calls are not allowed

Answer

The whole event loop is blocked; every other request on that worker stalls

Why

An async server uses ONE thread with an event loop. The loop switches to other requests whenever the current one is waiting at an await.

time.sleep and requests.get wait without await, so the loop cannot switch: the thread is frozen and every other user waits too.

Use async versions (await asyncio.sleep, httpx.AsyncClient) or declare the endpoint with plain def, which FastAPI runs in a separate thread.

Remember

Never call blocking code inside async def.

Key terms

Practice this question in Repaso