在FastAPI中获取后台任务的返回状态 [英] Get return status from Background Tasks in FastAPI
本文介绍了在FastAPI中获取后台任务的返回状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个API,可以发布创建后台作业的作业,我想在另一个GET API上发送作业的状态。如何做到这一点呢?在background_work()
函数中,我使用多处理,因为内部调用以subprocess.call()
调用为目标。
from fastapi import BackgroundTasks, FastAPI
app = FastAPI()
def background_work(data: str):
# some computation on data and return it
return status
@app.post("/post_job", status_code=HTTP_201_CREATED)
async def send_notification(data: str, background_tasks: BackgroundTasks):
background_tasks.add_task(background_work, data)
return {"message": "Job Created, check status after some time!"}
@app.get("/get_status")
def status():
#how to return status of job submitted to background task
推荐答案
目前无法使用FastAPI实现这一点,因为后台任务只是对发送响应后要调用的可调用函数的引用,它们不存储任何类型的状态。
您必须使用芹菜或其他库。
这篇关于在FastAPI中获取后台任务的返回状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文