FastAPI: a Python backend

Lesson 08 · 11 min

async, background tasks, and lifespan

What to do before startup, after the response, and while waiting on the network.

An async def route runs on the event loop: while it waits on await, the server handles other requests. For HTTP calls from inside, use the async client httpx.AsyncClient. BackgroundTasks runs a function after the response has gone to the client — for an email or a log entry. lifespan is code that runs once at app startup and shutdown: a connection pool, clients, caches.

Sign in to continue

The rest of this lesson opens after sign up

The first lesson of every course is open without an account. To read on, solve the practice and use Ask, create a free account or log in.