简体   繁体   English

检查 asyncio.get_event_loop() 是否已完成?

[英]Check if asyncio.get_event_loop() has finished?

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)

You can make a list of myfunction() tasks, then run them with asyncio.wait()您可以列出 myfunction() 任务,然后使用 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:注意get_event_loop()在python 3.10中是折旧的,所以最好的解决方案可能是在装饰器定义之前启动后台循环,然后直接使用循环名称:

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

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:在 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()

Then, in your wrapped function use this loop like this:然后,在你的包装函数中,像这样使用这个循环:

await loop.run_in_executor(...)

And remember use async and await expressions where appropriate.并记住在适当的地方使用asyncawait表达式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asyncio.get_event_loop(): DeprecationWarning: 没有当前事件循环 - asyncio.get_event_loop(): DeprecationWarning: There is no current event loop 如何替换 asyncio.get_event_loop() 以避免 DeprecationWarning? - How to replace asyncio.get_event_loop() to avoid the DeprecationWarning? 如果有两个 asyncio.get_event_loop,顺序是什么? - what's the order if there are two asyncio.get_event_loop? 何时使用 asyncio.get_running_loop() 与 asyncio.get_event_loop()? - When to use asyncio.get_running_loop() vs asyncio.get_event_loop()? 跟随 asyncio.run() 时 asyncio.get_event_loop() 失败 - asyncio.get_event_loop() fails when following asyncio.run() 我是否需要跟踪 asyncio 事件循环,或者我可以在需要时调用 asyncio.get_event_loop 吗? - Do I need to keep track of the asyncio event loop, or can I just call asyncio.get_event_loop when it is needed? 为什么asyncio.get_event_loop方法检查当前线程是否为主线程? - Why asyncio.get_event_loop method checks if the current thread is the main thread? 在没有 asyncio.get_event_loop() 的 run_in_executor 的异步方法中使用线程池 - Using Threadpool in an Async method without run_in_executor of asyncio.get_event_loop() 用 ayncio.run 替换 asyncio.get_event_loop().run_until_complete - Replacing asyncio.get_event_loop().run_until_complete with ayncio.run 在Python3.6.1中调用loop.close asyncio.get_event_loop后无法创建新的事件循环 - Can't create new event loop after calling loop.close asyncio.get_event_loop in Python3.6.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM