Debian Codegraph @

docs_src/background_tasks/tutorial001_py310.py

raw
1 from fastapi import BackgroundTasks, FastAPI
2
3 app = FastAPI()
4
5
6 def write_notification(email: str, message=""):
7 with open("log.txt", mode="w") as email_file:
8 content = f"notification for {email}: {message}"
9 email_file.write(content)
10
11
12 @app.post("/send-notification/{email}")
13 async def send_notification(email: str, background_tasks: BackgroundTasks):
14 background_tasks.add_task(write_notification, email, message="some notification")
15 return {"message": "Notification sent in the background"}