Coverage for docs_src/background_tasks/tutorial001.py: 100%
10 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
1from fastapi import BackgroundTasks, FastAPI 1abcde
3app = FastAPI() 1abcde
6def write_notification(email: str, message=""): 1abcde
7 with open("log.txt", mode="w") as email_file: 1fghij
8 content = f"notification for {email}: {message}" 1fghij
9 email_file.write(content) 1fghij
12@app.post("/send-notification/{email}") 1abcde
13async def send_notification(email: str, background_tasks: BackgroundTasks): 1abcde
14 background_tasks.add_task(write_notification, email, message="some notification") 1fghij
15 return {"message": "Notification sent in the background"} 1fghij