Browse Source

docs: add BackgroundTasks usage example

pull/14401/head
niral 8 months ago
parent
commit
ac4bfca4cf
  1. 19
      docs/en/docs/tutorial/background-tasks.md

19
docs/en/docs/tutorial/background-tasks.md

@ -19,6 +19,25 @@ First, import `BackgroundTasks` and define a parameter in your *path operation f
**FastAPI** will create the object of type `BackgroundTasks` for you and pass it as that parameter. **FastAPI** will create the object of type `BackgroundTasks` for you and pass it as that parameter.
### Example
```python
from fastapi import FastAPI, BackgroundTasks
app = FastAPI()
def write_log(message: str):
with open("log.txt", "a") as file:
file.write(message + "\n")
@app.post("/send-message/")
async def send_message(message: str, background_tasks: BackgroundTasks):
# This will run *after* sending the response
background_tasks.add_task(write_log, message)
return {"message": "Message received"}
```
## Create a task function { #create-a-task-function } ## Create a task function { #create-a-task-function }
Create a function to be run as the background task. Create a function to be run as the background task.

Loading…
Cancel
Save