2

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

4
  • 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. Commented Oct 20, 2022 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 Commented Oct 20, 2022 at 17:56
  • 2
    You may be looking for the run_in_executor method, which will wrap a synchronous function in a thread (or process) so that it doesn't block your event loop. Commented Oct 20, 2022 at 18:04
  • Does this answer your question? asyncio, wrapping a normal function as asynchronous Commented Oct 20, 2022 at 18:19

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.