๐ฅ ๐¶
๐ ๐ช ๐ฌ ๐ฅ ๐ ๐ โฎ๏ธ ๐ฌ ๐จ.
๐ โ ๐ ๏ธ ๐ ๐ช ๐จ โฎ๏ธ ๐จ, โ๏ธ ๐ ๐ฉโ๐ป ๐ซ ๐ค โ๏ธ โ ๐ ๏ธ ๐ โญ ๐จ ๐จ.
๐ ๐, ๐ผ:
- ๐ง ๐จ ๐จ โฎ๏ธ ๐ญ ๐ฏ:
- ๐ ๐ง ๐ฝ & ๐จ ๐ง ๐ "๐" (๐ ๐ฅ), ๐ ๐ช ๐จ ๐จ โถ๏ธ๏ธ โ๏ธ & ๐จ ๐ง ๐จ ๐ฅ.
- ๐ญ ๐ฝ:
- ๐ผ, โก๏ธ ๐ฌ ๐ ๐จ ๐ ๐ ๐ ๐ถ ๐ ๐ ๐ ๏ธ, ๐ ๐ช ๐จ ๐จ "๐ซ" (๐บ๐ธ๐ 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
โฎ๏ธ ๐ข โก ๐ ๏ธ ๐ข & ๐ ๐ฎ ๐ฅ ๐.