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:
from air import BackgroundTasks, Air
app = Air()
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):
message = "some notification"
background_tasks.add_task(write_notification, email, message=message)
content = f"notification for {email}: {message}"
return air.layouts.picocss(air.Title(content), air.H1(content))
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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|