有没有办法从 DELETE 端点结束我在 POST 中添加的 FastApi BackgroundTask?
例如:以FastAPI 教程中的示例为例:
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"}
Run Code Online (Sandbox Code Playgroud)
永久终止添加为后台任务的所有未完成调用的正确方法是什么write_notifaction
?