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

10 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1from fastapi import BackgroundTasks, FastAPI 1abcdef

2 

3app = FastAPI() 1abcdef

4 

5 

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

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

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

9 email_file.write(content) 1ghijkl

10 

11 

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

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

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

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