简体   繁体   中英

How to run async function in FastAPI only once at a time?

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM