1

I have a function that make a post request with a lot of treatment. All of that takes 30 seconds.

I need to execute this function every 6 mins. So I used asyncio for that ... But it's not asynchrone my api is blocked since the end of function ... Later I will have treatment that takes 5 minutes to execute.

def update_all():
    # do request and treatment (30 secs)

async run_update_all():
    while True:
        await asyncio.sleep(6 * 60)
        update_all()

loop = asyncio.get_event_loop()
loop.create_task(run_update_all())

So, I don't understand why during the execute time of update_all() all requests comming are in pending, waiting for the end of update_all() instead of being asynchronous

Chris
  • 18,724
  • 6
  • 46
  • 80
LelouchXV
  • 143
  • 1
  • 8
  • 4
    If `update_all()` is not an async function it will block the thread. It's not clear why you are expecting it not to. – Mark Oct 20 '22 at 17:23
  • Because I think there is a way to use a blocking function in a thread ? But I don't know how to do that – LelouchXV Oct 20 '22 at 17:56
  • 2
    You may be looking for the [`run_in_executor`](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor) method, which will wrap a synchronous function in a thread (or process) so that it doesn't block your event loop. – larsks Oct 20 '22 at 18:04
  • Does this answer your question? [asyncio, wrapping a normal function as asynchronous](https://stackoverflow.com/questions/57336602/asyncio-wrapping-a-normal-function-as-asynchronous) – SuperStormer Oct 20 '22 at 18:19

1 Answers1

3

I found an answer with the indication of larsks

I did that :

def update_all():
    # Do synchrone post request and treatment that take long time

async def launch_async():
    loop = asyncio.get_event_loop()
    while True:
        await asyncio.sleep(120)
        loop.run_in_executore(None, update_all)

asyncio.create_task(launch_async())

With that code I'm able to launch a synchrone function every X seconds without blocking the main thread of FastApi :D

I hope that will help other people in the same case than me.

LelouchXV
  • 143
  • 1
  • 8
  • Thanks this helped me get around the blocking behavior I was stuck on trying to get an "after startup" event to fire (basically launch this async task in startup event function with a timer) – clesiemo3 Jan 26 '23 at 15:59