I would like to make a request, which starts a process and returns before waiting until the started process is finished. With FastAPI it’s possible to archive this with background tasks:
app = FastAPI()
def long_process(id: str):
time.sleep(10)
print(f"{id} has been processed.")
@app.get("/")
async def test_bgtask(background_tasks: BackgroundTasks):
background_tasks.add_task(long_process, "1234")
return {"response before finish processing"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
If I combine FastAPI with Ray Serve then it didn’t worked. Here the response waits for the background task:
app = FastAPI()
def long_process(id: str):
time.sleep(10)
print(f"{id} has been processed.")
@app.get("/")
async def test_bgtask(background_tasks: BackgroundTasks):
background_tasks.add_task(long_process, "1234")
return {"response before finish processing"}
@serve.deployment(route_prefix="/")
@serve.ingress(app)
class FastAPIWrapper:
pass
serve.run(FastAPIWrapper.bind())
print("send request")
resp = requests.get("http://localhost:8000/")
print(resp)
Any idea how I can archive it?