new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Tutorial · all subjects

background-tasks

7 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

BackgroundTasks purpose and use cases

Background tasks in FastAPI run after returning a response to the client. They are useful for operations that don't need to complete before the response is sent. Common use cases include: sending email notifications after an action (return response immediately while email sends in the background), and processing data files (return HTTP 202 Accepted response while processing happens in the background).

Import and declare BackgroundTasks parameter

To use background tasks, import BackgroundTasks from fastapi and declare a parameter in your path operation function with type declaration of BackgroundTasks. FastAPI will create the BackgroundTasks object and pass it as that parameter.

Background task function requirements

A background task function is a standard function that can receive parameters. It can be either an async def or a normal def function; FastAPI handles both correctly. The function is called with the add_task() method before the response is returned.

BackgroundTasks.add_task() method signature

The add_task() method receives: (1) a task function to be run in the background, (2) any sequence of positional arguments that should be passed to the task function in order, and (3) any keyword arguments that should be passed to the task function.

BackgroundTasks with dependency injection

BackgroundTasks works with the dependency injection system. You can declare a BackgroundTasks parameter in a path operation function, in a dependency (dependable), in a sub-dependency, or at multiple levels. FastAPI reuses the same BackgroundTasks object across all levels and merges all background tasks together to run in the background after the response is sent.

BackgroundTasks vs BackgroundTask class name

The class BackgroundTasks (with 's') comes from starlette.background and is imported directly into FastAPI for convenient import from fastapi. This allows you to use it as a path operation function parameter and have FastAPI handle it automatically. The alternative BackgroundTask (without 's') exists in starlette.background but requires manual object creation and Starlette Response wrapping if used directly in FastAPI.

When to use BackgroundTasks vs Celery

Use BackgroundTasks for small background tasks like sending email notifications or when you need to access variables and objects from the same FastAPI app. Use larger tools like Celery for heavy background computation that doesn't need to run in the same process, when you don't need to share memory or variables, and when you need to run tasks across multiple processes or servers. Celery requires complex configuration and a message/job queue manager like RabbitMQ or Redis.

Give your agent this brain