Add better examples for Use BackgroundTasks

This commit is contained in:
Yerassyl Zhanymkanov
2022-08-19 01:39:03 +06:00
parent f38c816160
commit 158b55152d

View File

@@ -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 They are stable enough for async (delayed) tasks
```python ```python
from fastapi import BackgroundTasks from fastapi import BackgroundTasks
from pydantic import UUID4
from src.notifications import service as notifications_service
# router.py # router.py
@router.get("/users/{user_id}/posts/{post_id}") @router.post("/users/{user_id}/email")
async def get_user_post( async def send_user_email(worker: BackgroundTasks, user_id: UUID4):
worker: BackgroundTasks, """Send email to user"""
): worker.add_task(notifications_service.send_email, user_id) # send email after responding client
"""Get post that belong the active user."""
worker.add_task(notifications_service.send_email, user["id"])
return {"status": "ok"} return {"status": "ok"}
``` ```
### 19. Typing is important ### 19. Typing is important