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

10 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from fastapi import BackgroundTasks, FastAPI 1abcde

2 

3app = FastAPI() 1abcde

4 

5 

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

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

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

9 email_file.write(content) 1abcde

10 

11 

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") 1abcde

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