相关疑难解决方法(0)

从 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

Run Code Online (Sandbox Code Playgroud)

python-3.x fastapi

6
推荐指数
3
解决办法
8064
查看次数

标签 统计

fastapi ×1

python-3.x ×1