Skip to content

๐Ÿ–ฅ ๐Ÿ“‹

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ ๐Ÿ–ฅ ๐Ÿ“‹ ๐Ÿƒ โฎ๏ธ ๐Ÿ›ฌ ๐Ÿ“จ.

๐Ÿ‘‰ โš  ๐Ÿ› ๏ธ ๐Ÿ‘ˆ ๐Ÿ’ช ๐Ÿ”จ โฎ๏ธ ๐Ÿ“จ, โœ‹๏ธ ๐Ÿ‘ˆ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿšซ ๐Ÿค™ โœ”๏ธ โŒ› ๐Ÿ› ๏ธ ๐Ÿ โญ ๐Ÿ“จ ๐Ÿ“จ.

๐Ÿ‘‰ ๐Ÿ”Œ, ๐Ÿ–ผ:

  • ๐Ÿ“ง ๐Ÿ“จ ๐Ÿ“จ โฎ๏ธ ๐ŸŽญ ๐ŸŽฏ:
    • ๐Ÿ”— ๐Ÿ“ง ๐Ÿ’ฝ & ๐Ÿ“จ ๐Ÿ“ง ๐Ÿ˜‘ "๐ŸŒ" (๐Ÿ“š ๐Ÿฅˆ), ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ“จ โ–ถ๏ธ๏ธ โ†–๏ธ & ๐Ÿ“จ ๐Ÿ“ง ๐Ÿ“จ ๐Ÿ–ฅ.
  • ๐Ÿญ ๐Ÿ’ฝ:
    • ๐Ÿ–ผ, โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘† ๐Ÿ“จ ๐Ÿ“ ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿšถ ๐Ÿ”˜ ๐ŸŒ ๐Ÿ› ๏ธ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ“จ "๐Ÿšซ" (๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” 2๏ธโƒฃ0๏ธโƒฃ2๏ธโƒฃ) & ๐Ÿ› ๏ธ โšซ๏ธ ๐Ÿ–ฅ.

โš™๏ธ BackgroundTasks

๐Ÿฅ‡, ๐Ÿ—„ BackgroundTasks & ๐Ÿ”ฌ ๐Ÿ”ข ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข โฎ๏ธ ๐Ÿ†Ž ๐Ÿ“„ BackgroundTasks:

from fastapi import BackgroundTasks, FastAPI

app = FastAPI()


def write_notification(email: str, message=""):
    with open("log.txt", mode="w") as email_file:
        content = f"notification for {email}: {message}"
        email_file.write(content)


@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
    background_tasks.add_task(write_notification, email, message="some notification")
    return {"message": "Notification sent in the background"}

FastAPI ๐Ÿ”œ โœ ๐ŸŽš ๐Ÿ†Ž BackgroundTasks ๐Ÿ‘† & ๐Ÿšถโ€โ™€๏ธ โšซ๏ธ ๐Ÿ‘ˆ ๐Ÿ”ข.

โœ ๐Ÿ“‹ ๐Ÿ”ข

โœ ๐Ÿ”ข ๐Ÿƒ ๐Ÿ–ฅ ๐Ÿ“‹.

โšซ๏ธ ๐Ÿฉ ๐Ÿ”ข ๐Ÿ‘ˆ ๐Ÿ’ช ๐Ÿ“จ ๐Ÿ”ข.

โšซ๏ธ ๐Ÿ’ช async def โš–๏ธ ๐Ÿ˜ def ๐Ÿ”ข, FastAPI ๐Ÿ”œ ๐Ÿ’ญ โ” ๐Ÿต โšซ๏ธ โ˜‘.

๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿ“‹ ๐Ÿ”ข ๐Ÿ”œ โœ ๐Ÿ“ (โš– ๐Ÿ“จ ๐Ÿ“ง).

& โœ ๐Ÿ› ๏ธ ๐Ÿšซ โš™๏ธ async & await, ๐Ÿ‘ฅ ๐Ÿ”ฌ ๐Ÿ”ข โฎ๏ธ ๐Ÿ˜ def:

from fastapi import BackgroundTasks, FastAPI

app = FastAPI()


def write_notification(email: str, message=""):
    with open("log.txt", mode="w") as email_file:
        content = f"notification for {email}: {message}"
        email_file.write(content)


@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
    background_tasks.add_task(write_notification, email, message="some notification")
    return {"message": "Notification sent in the background"}

๐Ÿšฎ ๐Ÿ–ฅ ๐Ÿ“‹

๐Ÿ”˜ ๐Ÿ‘† โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, ๐Ÿšถโ€โ™€๏ธ ๐Ÿ‘† ๐Ÿ“‹ ๐Ÿ”ข ๐Ÿ–ฅ ๐Ÿ“‹ ๐ŸŽš โฎ๏ธ ๐Ÿ‘ฉโ€๐Ÿ”ฌ .add_task():

from fastapi import BackgroundTasks, FastAPI

app = FastAPI()


def write_notification(email: str, message=""):
    with open("log.txt", mode="w") as email_file:
        content = f"notification for {email}: {message}"
        email_file.write(content)


@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
    background_tasks.add_task(write_notification, email, message="some notification")
    return {"message": "Notification sent in the background"}

.add_task() ๐Ÿ“จ โŒ:

  • ๐Ÿ“‹ ๐Ÿ”ข ๐Ÿƒ ๐Ÿ–ฅ (write_notification).
  • ๐Ÿ™† ๐Ÿ” โŒ ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿšถโ€โ™€๏ธ ๐Ÿ“‹ ๐Ÿ”ข โœ” (email).
  • ๐Ÿ™† ๐Ÿ‡จ๐Ÿ‡ป โŒ ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿšถโ€โ™€๏ธ ๐Ÿ“‹ ๐Ÿ”ข (message="some notification").

๐Ÿ”— ๐Ÿ’‰

โš™๏ธ BackgroundTasks ๐Ÿ‘ท โฎ๏ธ ๐Ÿ”— ๐Ÿ’‰ โš™๏ธ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ”ข ๐Ÿ†Ž BackgroundTasks ๐Ÿ’— ๐ŸŽš: โžก ๐Ÿ› ๏ธ ๐Ÿ”ข, ๐Ÿ”— (โ˜‘), ๐ŸŽง-๐Ÿ”—, โ™’๏ธ.

FastAPI ๐Ÿ’ญ โšซ๏ธโ” ๐Ÿ”  ๐Ÿ’ผ & โ” ๐Ÿค-โš™๏ธ ๐ŸŽ ๐ŸŽš, ๐Ÿ‘ˆ ๐ŸŒ ๐Ÿ–ฅ ๐Ÿ“‹ ๐Ÿ”— ๐Ÿ‘ฏโ€โ™‚๏ธ & ๐Ÿƒ ๐Ÿ–ฅ โฎ๏ธ:

from typing import Union

from fastapi import BackgroundTasks, Depends, FastAPI

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: str = Depends(get_query)
):
    message = f"message to {email}\n"
    background_tasks.add_task(write_log, message)
    return {"message": "Message sent"}
from fastapi import BackgroundTasks, Depends, FastAPI

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: 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: str = Depends(get_query)
):
    message = f"message to {email}\n"
    background_tasks.add_task(write_log, message)
    return {"message": "Message sent"}

๐Ÿ‘‰ ๐Ÿ–ผ, ๐Ÿ“ง ๐Ÿ”œ โœ log.txt ๐Ÿ“ โฎ๏ธ ๐Ÿ“จ ๐Ÿ“จ.

๐Ÿšฅ ๐Ÿ“ค ๐Ÿ”ข ๐Ÿ“จ, โšซ๏ธ ๐Ÿ”œ โœ ๐Ÿ•น ๐Ÿ–ฅ ๐Ÿ“‹.

& โคด๏ธ โž•1๏ธโƒฃ ๐Ÿ–ฅ ๐Ÿ“‹ ๐Ÿ— โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ”œ โœ ๐Ÿ“ง โš™๏ธ email โžก ๐Ÿ”ข.

๐Ÿ“ก โ„น

๐ŸŽ“ BackgroundTasks ๐Ÿ‘Ÿ ๐Ÿ”— โšช๏ธโžก๏ธ starlette.background.

โšซ๏ธ ๐Ÿ—„/๐Ÿ”Œ ๐Ÿ”— ๐Ÿ”˜ FastAPI ๐Ÿ‘ˆ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ—„ โšซ๏ธ โšช๏ธโžก๏ธ fastapi & โŽ ๐Ÿ˜ซ ๐Ÿ—„ ๐ŸŽ› BackgroundTask (๐Ÿต s ๐Ÿ”š) โšช๏ธโžก๏ธ starlette.background.

๐Ÿ•ด โš™๏ธ BackgroundTasks (& ๐Ÿšซ BackgroundTask), โšซ๏ธ โคด๏ธ ๐Ÿ’ช โš™๏ธ โšซ๏ธ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข ๐Ÿ”ข & โœ”๏ธ FastAPI ๐Ÿต ๐ŸŽ‚ ๐Ÿ‘†, ๐Ÿ’– ๐Ÿ•โ” โš™๏ธ Request ๐ŸŽš ๐Ÿ”—.

โšซ๏ธ ๐Ÿ’ช โš™๏ธ BackgroundTask ๐Ÿ˜ž FastAPI, โœ‹๏ธ ๐Ÿ‘† โœ”๏ธ โœ ๐ŸŽš ๐Ÿ‘† ๐Ÿ“Ÿ & ๐Ÿ“จ ๐Ÿ’ƒ Response ๐Ÿ”Œ โšซ๏ธ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ‘€ ๐ŸŒ– โ„น ๐Ÿ’ƒ ๐Ÿ›‚ ๐Ÿฉบ ๐Ÿ–ฅ ๐Ÿ“‹.

โš 

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’ช ๐ŸŽญ ๐Ÿ‹๏ธ ๐Ÿ–ฅ ๐Ÿ“Š & ๐Ÿ‘† ๐Ÿšซ ๐ŸŽฏ ๐Ÿ’ช โšซ๏ธ ๐Ÿƒ ๐ŸŽ ๐Ÿ› ๏ธ (๐Ÿ–ผ, ๐Ÿ‘† ๐Ÿšซ ๐Ÿ’ช ๐Ÿ’ฐ ๐Ÿ’พ, ๐Ÿ”ข, โ™’๏ธ), ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ฐ โšช๏ธโžก๏ธ โš™๏ธ ๐ŸŽ ๐Ÿฆ ๐Ÿงฐ ๐Ÿ’– ๐Ÿฅ’.

๐Ÿ‘ซ ๐Ÿ˜‘ ๐Ÿšš ๐ŸŒ– ๐Ÿ— ๐Ÿ“ณ, ๐Ÿ“ง/๐Ÿ‘จโ€๐Ÿญ ๐Ÿ“ค ๐Ÿ‘จโ€๐Ÿ’ผ, ๐Ÿ’– โœณ โš–๏ธ โœณ, โœ‹๏ธ ๐Ÿ‘ซ โœ” ๐Ÿ‘† ๐Ÿƒ ๐Ÿ–ฅ ๐Ÿ“‹ ๐Ÿ’— ๐Ÿ› ๏ธ, & โœด๏ธ, ๐Ÿ’— ๐Ÿ’ฝ.

๐Ÿ‘€ ๐Ÿ–ผ, โœ… ๐Ÿ— ๐Ÿš‚, ๐Ÿ‘ซ ๐ŸŒ ๐Ÿ”Œ ๐Ÿฅ’ โช ๐Ÿ“ถ.

โœ‹๏ธ ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ” ๐Ÿ”ข & ๐ŸŽš โšช๏ธโžก๏ธ ๐ŸŽ FastAPI ๐Ÿ“ฑ, โš–๏ธ ๐Ÿ‘† ๐Ÿ’ช ๐ŸŽญ ๐Ÿคช ๐Ÿ–ฅ ๐Ÿ“‹ (๐Ÿ’– ๐Ÿ“จ ๐Ÿ“ง ๐Ÿ“จ), ๐Ÿ‘† ๐Ÿ’ช ๐ŸŽฏ โš™๏ธ BackgroundTasks.

๐ŸŒƒ

๐Ÿ—„ & โš™๏ธ BackgroundTasks โฎ๏ธ ๐Ÿ”ข โžก ๐Ÿ› ๏ธ ๐Ÿ”ข & ๐Ÿ”— ๐Ÿšฎ ๐Ÿ–ฅ ๐Ÿ“‹.