I'm new to FastAPI and asyncio and don't know how to realise this. I have an endpoint, when called, it should start an AI prediction that takes about 40s in the background.
async def asyncio_test_prediction():
print("Starting asnycio func")
time.sleep(30);
print("Stopping asyncio func")
@app.get('/sempos/start')
async def start_prediction():
asyncio.ensure_future(asyncio_test_prediction())
return {
"state": "Started"
}
This is the only way I managed for it to work. The EP function doesn't really need to be async but without it it doesn't work.
Now, I also want to ensure that "asyncio_test_prediction" is only called when it's not already running.
I read that this is possible with task.done()
but I'm not sure how to initialize and store it. Or is there a better option?
Thanks for your help.