MCPcopy
hub / github.com/fastapi/fastapi / BackgroundTasks

Class BackgroundTasks

fastapi/background.py:11–61  ·  view source on GitHub ↗

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 Backgrou

Source from the content-addressed store, hash-verified

9
10
11class BackgroundTasks(StarletteBackgroundTasks):
12 """
13 A collection of background tasks that will be called after a response has been
14 sent to the client.
15
16 Read more about it in the
17 [FastAPI docs for Background Tasks](https://fastapi.tiangolo.com/tutorial/background-tasks/).
18
19 ## Example
20
21 ```python
22 from fastapi import BackgroundTasks, FastAPI
23
24 app = FastAPI()
25
26
27 def write_notification(email: str, message=""):
28 with open("log.txt", mode="w") as email_file:
29 content = f"notification for {email}: {message}"
30 email_file.write(content)
31
32
33 @app.post("/send-notification/{email}")
34 async def send_notification(email: str, background_tasks: BackgroundTasks):
35 background_tasks.add_task(write_notification, email, message="some notification")
36 return {"message": "Notification sent in the background"}
37 ```
38 """
39
40 def add_task(
41 self,
42 func: Annotated[
43 Callable[P, Any],
44 Doc(
45 """
46 The function to call after the response is sent.
47
48 It can be a regular `def` function or an `async def` function.
49 """
50 ),
51 ],
52 *args: P.args,
53 **kwargs: P.kwargs,
54 ) -> None:
55 """
56 Add a function to be called in the background after the response is sent.
57
58 Read more about it in the
59 [FastAPI docs for Background Tasks](https://fastapi.tiangolo.com/tutorial/background-tasks/).
60 """
61 return super().add_task(func, *args, **kwargs)

Callers 1

solve_dependenciesFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…