Coverage for docs_src/background_tasks/tutorial001.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1from fastapi import BackgroundTasks, FastAPI 1abcdefg

2 

3app = FastAPI() 1abcdefg

4 

5 

6def write_notification(email: str, message=""): 1abcdefg

7 with open("log.txt", mode="w") as email_file: 1hijklmn

8 content = f"notification for {email}: {message}" 1hijklmn

9 email_file.write(content) 1hijklmn

10 

11 

12@app.post("/send-notification/{email}") 1abcdefg

13async def send_notification(email: str, background_tasks: BackgroundTasks): 1abcdefg

14 background_tasks.add_task(write_notification, email, message="some notification") 1hijklmn

15 return {"message": "Notification sent in the background"} 1hijklmn