mirror of https://github.com/tiangolo/fastapi.git
Remove code examples for Python 3.8 in `background_tasks`
This commit is contained in:
parent
3b563725ee
commit
c60424337e
|
|
@ -15,7 +15,7 @@ This includes, for example:
|
|||
|
||||
First, import `BackgroundTasks` and define a parameter in your *path operation function* with a type declaration of `BackgroundTasks`:
|
||||
|
||||
{* ../../docs_src/background_tasks/tutorial001.py hl[1,13] *}
|
||||
{* ../../docs_src/background_tasks/tutorial001_py39.py hl[1,13] *}
|
||||
|
||||
**FastAPI** will create the object of type `BackgroundTasks` for you and pass it as that parameter.
|
||||
|
||||
|
|
@ -31,13 +31,13 @@ In this case, the task function will write to a file (simulating sending an emai
|
|||
|
||||
And as the write operation doesn't use `async` and `await`, we define the function with normal `def`:
|
||||
|
||||
{* ../../docs_src/background_tasks/tutorial001.py hl[6:9] *}
|
||||
{* ../../docs_src/background_tasks/tutorial001_py39.py hl[6:9] *}
|
||||
|
||||
## Add the background task { #add-the-background-task }
|
||||
|
||||
Inside of your *path operation function*, pass your task function to the *background tasks* object with the method `.add_task()`:
|
||||
|
||||
{* ../../docs_src/background_tasks/tutorial001.py hl[14] *}
|
||||
{* ../../docs_src/background_tasks/tutorial001_py39.py hl[14] *}
|
||||
|
||||
`.add_task()` receives as arguments:
|
||||
|
||||
|
|
|
|||
|
|
@ -1,27 +0,0 @@
|
|||
from typing import Union
|
||||
|
||||
from fastapi import BackgroundTasks, Depends, FastAPI
|
||||
from typing_extensions import Annotated
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
def write_log(message: str):
|
||||
with open("log.txt", mode="a") as log:
|
||||
log.write(message)
|
||||
|
||||
|
||||
def get_query(background_tasks: BackgroundTasks, q: Union[str, None] = None):
|
||||
if q:
|
||||
message = f"found query: {q}\n"
|
||||
background_tasks.add_task(write_log, message)
|
||||
return q
|
||||
|
||||
|
||||
@app.post("/send-notification/{email}")
|
||||
async def send_notification(
|
||||
email: str, background_tasks: BackgroundTasks, q: Annotated[str, Depends(get_query)]
|
||||
):
|
||||
message = f"message to {email}\n"
|
||||
background_tasks.add_task(write_log, message)
|
||||
return {"message": "Message sent"}
|
||||
|
|
@ -3,7 +3,7 @@ from pathlib import Path
|
|||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from docs_src.background_tasks.tutorial001 import app
|
||||
from docs_src.background_tasks.tutorial001_py39 import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
|
|
|||
|
|
@ -5,16 +5,15 @@ from pathlib import Path
|
|||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ...utils import needs_py39, needs_py310
|
||||
from ...utils import needs_py310
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="client",
|
||||
params=[
|
||||
"tutorial002",
|
||||
"tutorial002_py39",
|
||||
pytest.param("tutorial002_py310", marks=needs_py310),
|
||||
"tutorial002_an",
|
||||
pytest.param("tutorial002_an_py39", marks=needs_py39),
|
||||
"tutorial002_an_py39",
|
||||
pytest.param("tutorial002_an_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in New Issue