Add better examples for Use BackgroundTasks
This commit is contained in:
13
README.md
13
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
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user