我有一个自定义的背景装饰器。当我在后台运行一个函数时,我如何检查我在后台启动的所有函数是否都完成了,直到我返回?
#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)发布于 2021-10-18 00:36:04
在main函数之外,您应该定义您的循环,并使用此函数等待将在main中运行的所有函数,直到它们完成:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main()) # <- This is what you are looking for
loop.close()然后,在您的包装函数中使用如下循环:
await loop.run_in_executor(...)记住在适当的地方使用async和await表达式。
https://stackoverflow.com/questions/69609270
复制相似问题