Background Tasks
noun
say it: “BAK-grownd TASKS” /ˈbækˌɡraʊnd ˈtæsks/
FastAPI's BackgroundTasks, which runs a function after the answer has been sent, so the caller doesn't wait for work such as sending an email.
What is Background Tasks?
Background tasks run after the response is sent, such as emailing a receipt, so the user doesn't wait.
@app.post('/orders')
def place_order(order: OrderIn, tasks: BackgroundTasks):
saved = save_order(order)
tasks.add_task(send_receipt, saved.email)
return saved
In a sentence
- “Background tasks live in memory, so a restart loses any still waiting.”
- “The vote route answers at once and sends the confirmation with background tasks.”
Level: Journeyman · More FastAPI terms at this level
Updated