Middleware
plateforme.core.api.middleware
This module provides utilities for managing middleware within the Plateforme framework's API using FastAPI and Starlette features.
plateforme.core.api.background
This module provides utilities for managing background tasks within the Plateforme framework's API using FastAPI and Starlette features.
BackgroundTasks
BackgroundTasks(
tasks: Sequence[BackgroundTask] | None = 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"}
Source code in .venv/lib/python3.12/site-packages/starlette/background.py
add_task
add_task(
func: Annotated[
Callable[P, Any],
Doc(
"\n The function to call after the response is sent.\n\n It can be a regular `def` function or an `async def` function.\n "
),
],
*args: args,
**kwargs: 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.