Background Tasks
Background Tasks are Python functions that are called after a response is returned to the user. They are useful for long running tasks like emails or batch processing.
Background tasks in Air, for those times a long-running process is needed that doesn't force the user to wait.
BackgroundTasks
Bases: BackgroundTasks
A collection of background tasks that will be called after a response has been sent to the client.
Example:
import pathlib
import air
app = air.Air()
def write_notification(email: str, message=""):
with pathlib.Path("log.txt").open(mode="w") as email_file:
content = f"notification for {email}: {message}"
email_file.write(content)
@app.post("/send-notification/{email}")
def send_notification(email: str, background_tasks: air.BackgroundTasks):
message = "some notification"
background_tasks.add_task(write_notification, email, message=message)
return air.P(f"Notification sent to {email}")
add_task
add_task(func, *args, **kwargs)
Add a function to be called in the background after the response is sent.
Source code in src/air/background.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |