Browse Source

Remove code examples for Python 3.8 in `background_tasks`

pull/14510/head
Yurii Motov 7 months ago
parent
commit
c60424337e
  1. 6
      docs/en/docs/tutorial/background-tasks.md
  2. 0
      docs_src/background_tasks/tutorial001_py39.py
  3. 27
      docs_src/background_tasks/tutorial002_an.py
  4. 0
      docs_src/background_tasks/tutorial002_py39.py
  5. 2
      tests/test_tutorial/test_background_tasks/test_tutorial001.py
  6. 7
      tests/test_tutorial/test_background_tasks/test_tutorial002.py

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

@ -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:

0
docs_src/background_tasks/tutorial001.py → docs_src/background_tasks/tutorial001_py39.py

27
docs_src/background_tasks/tutorial002_an.py

@ -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"}

0
docs_src/background_tasks/tutorial002.py → docs_src/background_tasks/tutorial002_py39.py

2
tests/test_tutorial/test_background_tasks/test_tutorial001.py

@ -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)

7
tests/test_tutorial/test_background_tasks/test_tutorial002.py

@ -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…
Cancel
Save