কনটেন্টে যান

Background Tasks - BackgroundTasks

You can declare a parameter in a path operation function or dependency function with the type BackgroundTasks, and then you can use it to schedule the execution of background tasks after the response is sent.

You can import it directly from fastapi:

from fastapi import BackgroundTasks

fastapi.BackgroundTasks

BackgroundTasks(tasks=None)

Bases: BackgroundTasks

A collection of background tasks that will be called after a response has been sent to the client.

Read more about it in the FastAPI docs for Background Tasks.

Example

from fastapi import BackgroundTasks, FastAPI

app = FastAPI()


def write_notification(email: str, message=""):
    with open("log.txt", mode="w") as email_file:
        content = f"notification for {email}: {message}"
        email_file.write(content)


@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
    background_tasks.add_task(write_notification, email, message="some notification")
    return {"message": "Notification sent in the background"}
PARAMETER DESCRIPTION
tasks

TYPE: Optional[Sequence[BackgroundTask]] DEFAULT: None

Source code in starlette/background.py
32
33
def __init__(self, tasks: typing.Optional[typing.Sequence[BackgroundTask]] = None):
    self.tasks = list(tasks) if tasks else []

func instance-attribute

func = func

args instance-attribute

args = args

kwargs instance-attribute

kwargs = kwargs

is_async instance-attribute

is_async = is_async_callable(func)

tasks instance-attribute

tasks = list(tasks) if tasks else []

add_task

add_task(func, *args, **kwargs)

Add a function to be called in the background after the response is sent.

Read more about it in the FastAPI docs for Background Tasks.

PARAMETER DESCRIPTION
func

The function to call after the response is sent.

It can be a regular def function or an async def function.

TYPE: Callable[P, Any]

*args

TYPE: args DEFAULT: ()

**kwargs

TYPE: kwargs DEFAULT: {}

Source code in fastapi/background.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def add_task(
    self,
    func: Annotated[
        Callable[P, Any],
        Doc(
            """
            The function to call after the response is sent.

            It can be a regular `def` function or an `async def` function.
            """
        ),
    ],
    *args: P.args,
    **kwargs: P.kwargs,
) -> None:
    """
    Add a function to be called in the background after the response is sent.

    Read more about it in the
    [FastAPI docs for Background Tasks](https://fastapi.tiangolo.com/tutorial/background-tasks/).
    """
    return super().add_task(func, *args, **kwargs)