Issue
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)
Solution
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 deprecated 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
Answered By - Max Behling
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.