Tips and Tricks

FastAPI

Asynchronous Background Tasks in FastAPI


FastAPI tip:

You can use FastAPI's BackGround Tasks to run simple tasks in the background.

👇

from fastapi import BackgroundTasks


def send_email(email, message):
    pass


@app.get("/")
async def ping(background_tasks: BackgroundTasks):
    background_tasks.add_task(send_email, "[email protected]", "Hi!")
    return {"message": "pong!"}

Use Celery for CPU intensive tasks and when you need a task queue.