Concepts 🚀 Project 🧮 Labs
📚 Learn / Python / async / await

async / await

Coroutines that yield control to the event loop. FastAPI route handlers are async so they don't block other requests.

Code example

async def fetch_user(uid: int):
    user = await db.fetch_one(...)  # non-blocking
    return user

# bad — blocks the event loop
# def fetch_user(uid): time.sleep(5)

How it works

An async function (coroutine) returns immediately and resumes later when its awaited operation completes. While it's waiting (e.g. for a DB query), the event loop can run other coroutines — that's how a single Uvicorn worker can handle thousands of concurrent requests. The rule: if a function does I/O (DB, HTTP, file), make it async and await the I/O. CPU-bound work (like running a model benchmark) should be offloaded to a thread pool via `run_in_executor`.

🔍 Where this is used in OpenBenchML Every FastAPI route. Notebook cell execution uses `run_in_executor` so the model fit doesn't block the event loop.