1

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.

Tropaion
  • 67
  • 7

1 Answers1

-1

I suggest changing the expected behavior of your API.

How about having two endpoints:

  • '/sempos/start' - starts the AI prediction and immediately returns the id of the calculation
  • '/sempos/result/{id}' - gives a generic message if calculations aren't done, else gives the calculation results JSON

This way you can:

  • Leverage the speed of FastAPI
  • Use the flexibility of asyncio
  • Fit your use case

What do you think?

scr
  • 853
  • 1
  • 2
  • 14
  • I will look into it, hope I can manage. But to which ID are you refering to? And do you maybe know of an example with similar procedure? – Tropaion Nov 30 '22 at 12:55