Home > Back-end >  Check if asyncio.get_event_loop() has finished?
Check if asyncio.get_event_loop() has finished?

Time:10-18

I have a custom background decorator. When I run a function in the background, how can I then check if all functions, that I started in the background, are done until I return?

#background decorator
def background(fn):
    def wrapped(*args, **kwargs):
        return asyncio.get_event_loop().run_in_executor(None, fn, *args, **kwargs)
    return wrapped
#myfunction that I run in the background
@background
def myfunction():
    #run some tasks
#main here I call "myfunction"
def main():
    for i in list:
        myfunction()
#check if all myfunction's are done then return (MY PROBLEM)

CodePudding user response:

You can make a list of myfunction() tasks, then run them with asyncio.wait()

import asyncio
from timeit import default_timer as timer

def background(fn):
    def wrapped(*args, **kwargs):
        return asyncio.get_event_loop().run_in_executor(None, fn, *args,**kwargs)
    return wrapped

@background
def myfunction(tasknum):
    print("tasknum",tasknum," -- started at", timer())
    #add lots of numbers to simulate some task...
    x = 0
    for n in range(20000000):
        x  = n
    print("tasknum",tasknum," -- finished at", timer())

def main():
    print("main -- started at", timer())
    background_loop = asyncio.get_event_loop()
    tasks = []

    for i in range(4):
        tasks.append(myfunction(i))

    try:
        background_loop.run_until_complete(asyncio.wait(tasks))
    finally:
        background_loop.close()

    print("main -- finished at", timer())

main()
print('returned from main')

output:

main -- started at 38203.24129425
tasknum 0  -- started at 38203.241935683
tasknum 1  -- started at 38203.24716722
tasknum 2  -- started at 38203.257414232
tasknum 3  -- started at 38203.257518981
tasknum 1  -- finished at 38206.503383425
tasknum 2  -- finished at 38206.930789807
tasknum 0  -- finished at 38207.636296604
tasknum 3  -- finished at 38207.833483453
main -- finished at 38207.833736195
returned from main

Note that get_event_loop() is depreciated in python 3.10, so the best solution is probably to start the background loop before the decorator definition, and then just use the loop name directly:

background_loop = asyncio.new_event_loop()

def background(fn):  
    def wrapped(*args, **kwargs):
        return background_loop.run_in_executor(None, fn, *args, **kwargs)
    return wrapped

CodePudding user response:

Outside your main function you should define your loop, and use this to wait all the functions that will run in your main until they're completed:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())  # <- This is what you are looking for
loop.close()

Then, in your wrapped function use this loop like this:

await loop.run_in_executor(...)

And remember use async and await expressions where appropriate.

  • Related