fastapi后台任务kill

w6lpcovy  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(415)

有没有办法从删除端点结束我在帖子中添加的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 作为后台任务添加的呼叫?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题