🖥 📋¶
👆 💪 🔬 🖥 📋 🏃 ⏮️ 🛬 📨.
👉 ⚠ 🛠️ 👈 💪 🔨 ⏮️ 📨, ✋️ 👈 👩💻 🚫 🤙 ✔️ ⌛ 🛠️ 🏁 ⏭ 📨 📨.
👉 🔌, 🖼:
- 📧 📨 📨 ⏮️ 🎭 🎯:
- 🔗 📧 💽 & 📨 📧 😑 "🐌" (📚 🥈), 👆 💪 📨 📨 ▶️️ ↖️ & 📨 📧 📨 🖥.
- 🏭 💽:
- 🖼, ➡️ 💬 👆 📨 📁 👈 🔜 🚶 🔘 🐌 🛠️, 👆 💪 📨 📨 "🚫" (🇺🇸🔍 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"}
🤓 Other versions and variants
from typing import Annotated
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: Annotated[str, Depends(get_query)]
):
message = f"message to {email}\n"
background_tasks.add_task(write_log, message)
return {"message": "Message sent"}
from typing import Annotated, 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: Annotated[str, Depends(get_query)]
):
message = f"message to {email}\n"
background_tasks.add_task(write_log, message)
return {"message": "Message sent"}
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"}
Tip
Prefer to use the Annotated
version if possible.
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
⏮️ 🔢 ➡ 🛠️ 🔢 & 🔗 🚮 🖥 📋.