From 158b55152d07e01f0e245667d6999332b7e7552f Mon Sep 17 00:00:00 2001 From: Yerassyl Zhanymkanov Date: Fri, 19 Aug 2022 01:39:03 +0600 Subject: [PATCH] Add better examples for Use BackgroundTasks --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d9eff01..12afe92 100644 --- a/README.md +++ b/README.md @@ -536,15 +536,16 @@ Unless you have sync db connection (excuse me?) or aren't planning to write inte They are stable enough for async (delayed) tasks ```python from fastapi import BackgroundTasks +from pydantic import UUID4 + +from src.notifications import service as notifications_service # router.py -@router.get("/users/{user_id}/posts/{post_id}") -async def get_user_post( - worker: BackgroundTasks, -): - """Get post that belong the active user.""" - worker.add_task(notifications_service.send_email, user["id"]) +@router.post("/users/{user_id}/email") +async def send_user_email(worker: BackgroundTasks, user_id: UUID4): + """Send email to user""" + worker.add_task(notifications_service.send_email, user_id) # send email after responding client return {"status": "ok"} ``` ### 19. Typing is important