如何将Python并发未来与decorator一起使用

2024-09-24 20:31:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用线程池执行器的装饰器:

from functools import wraps
from .bounded_pool_executor import BoundedThreadPoolExecutor

_DEFAULT_POOL = BoundedThreadPoolExecutor(max_workers=5)

def threadpool(f, executor=None):
    @wraps(f)
    def wrap(*args, **kwargs):
        return (executor or _DEFAULT_POOL).submit(f, *args, **kwargs)

其中BoundedThreadPoolExecutor定义为here

当我尝试在一个用@threadpool修饰的函数中使用并发未来,然后用as_completed等着所有的未来

def get_results_as_completed(futures):
    # finished, pending = wait(futures, return_when=ALL_COMPLETED)
    futures_results = as_completed(futures)
    for f in futures_results:
        try:
            yield f.result()
        except:
            pass

对某些工人来说

from thread_support import threadpool
from time import sleep
from random import randint

@threadpool
def my_worker:
     res = {}
     # do something
     sleep(randint(1, 5))
     return res

if __name__ == "__main__":
   futures_results = get_results_as_completed(futures)
       for r in futures_results:
           results.append(r)

尽管有.result()调用,但我无法完成futures,从而导致futures结果的无限循环。为什么?你知道吗


Tags: fromimportdefaultreturndefasargsresults