我有一个 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)