Pep*_*epe 6 python multithreading background 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"}
Run Code Online (Sandbox Code Playgroud)
永久终止添加为后台任务的所有未完成调用的正确方法是什么write_notifaction
?
我认为 FastApi 目前默认不可能有一个优雅的解决方案。
您可以使用Celery来完成此任务。
看看这个例子。他们解决了 Celery 与 BackgroundTasks 之间的区别。
此外,您还可以在此处找到有关此主题的更多详细信息。
归档时间: |
|
查看次数: |
6632 次 |
最近记录: |