小编Pep*_*epe的帖子

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

python multithreading background fastapi

6
推荐指数
1
解决办法
6632
查看次数

标签 统计

background ×1

fastapi ×1

multithreading ×1

python ×1