BackgroundTasks runs functions after the response has been sent, which is good for quick follow-up work such as sending an email or writing a log.
They run in the same process with no retries or persistence, so if the process crashes the task is lost; use a real queue (Celery, RQ, Arq or a cloud queue) for work that is long, CPU-heavy, must be retried or must not be lost, such as payments, file conversion or model training.
from fastapi import BackgroundTasks
@app.post("/signup")
def signup(email: str, background_tasks: BackgroundTasks):
user = create_user(email)
background_tasks.add_task(send_welcome_email, email) # runs after the response
return {"status": "created"}