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 ⏮️ 🔢 ➡ 🛠️ 🔢 & 🔗 🚮 🖥 📋.