python - FastApi 后台任务杀死
问题描述
有没有办法从 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"}
永久终止write_notifaction
作为后台任务添加的所有未完成调用的正确方法是什么?
解决方案
推荐阅读
- python - 对图形工具顶点的 pagerank 值进行排序
- php - Laravel nova 动作下载文件导致:失败 - 没有文件
- angular - 使用导航中使用路由选择的页面
- c# - 如何在 C# 中正确触发事件
- excel - Excel-VBA ListBox Item to String 函数
- sql - 创建一个组合两个表的视图 ms sql server 2014
- sqlite - UPDATE table SET column=DEFAULT 不工作
- azure - 系统组成员身份无法更改
- python - 如何在 Python 中使用 Selenium 定位具有特定样式的 div?
- functional-programming - 函数式编程依赖关系:如何在未直接连接在一起的函数之间共享依赖关系