Home / File/ background.py — fastapi Source File

background.py — fastapi Source File

Architecture documentation for background.py, a python file in the fastapi codebase. 4 imports, 2 dependents.

File python FastAPI Applications 4 imports 2 dependents 1 classes

Entity Profile

Dependency Diagram

graph LR
  5bca856a_f0af_e71f_c4ca_cb6510fbc659["background.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  5bca856a_f0af_e71f_c4ca_cb6510fbc659 --> 0dda2280_3359_8460_301c_e98c77e78185
  5efacb44_5373_ceb9_9579_6e6603820488["annotated_doc"]
  5bca856a_f0af_e71f_c4ca_cb6510fbc659 --> 5efacb44_5373_ceb9_9579_6e6603820488
  b95bedab_6f00_fa4b_9342_419bd1cd9b7d["starlette.background"]
  5bca856a_f0af_e71f_c4ca_cb6510fbc659 --> b95bedab_6f00_fa4b_9342_419bd1cd9b7d
  87f0eda4_1c7f_c164_42ba_f715b8cf0a6b["typing_extensions"]
  5bca856a_f0af_e71f_c4ca_cb6510fbc659 --> 87f0eda4_1c7f_c164_42ba_f715b8cf0a6b
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  534f6e44_61b8_3c38_8b89_6934a6df9802 --> 5bca856a_f0af_e71f_c4ca_cb6510fbc659
  9e602cbf_3139_86ae_5666_97b8806942de["utils.py"]
  9e602cbf_3139_86ae_5666_97b8806942de --> 5bca856a_f0af_e71f_c4ca_cb6510fbc659
  style 5bca856a_f0af_e71f_c4ca_cb6510fbc659 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Annotated, Any, Callable

from annotated_doc import Doc
from starlette.background import BackgroundTasks as StarletteBackgroundTasks
from typing_extensions import ParamSpec

P = ParamSpec("P")


class BackgroundTasks(StarletteBackgroundTasks):
    """
    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](https://fastapi.tiangolo.com/tutorial/background-tasks/).

    ## Example

    ```python
    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"}
    ```
    """

    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)

Domain

Subdomains

Classes

Dependencies

  • annotated_doc
  • starlette.background
  • typing
  • typing_extensions

Frequently Asked Questions

What does background.py do?
background.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Applications subdomain.
What does background.py depend on?
background.py imports 4 module(s): annotated_doc, starlette.background, typing, typing_extensions.
What files import background.py?
background.py is imported by 2 file(s): __init__.py, utils.py.
Where is background.py in the architecture?
background.py is located at fastapi/background.py (domain: FastAPI, subdomain: Applications, directory: fastapi).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free