How to use Python Concurrent Futures with decorators [closed]

孤人 提交于 2019-12-07 23:29:28

问题


I'm using a decorator for the thread pool executor:

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)

where the BoundedThreadPoolExecutor is defined here

When I try to use the concurrent futures in a function decorated with @threadpool and then waiting all the futures withas_completed like

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

for some worker defined like

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)

I cannot get the futures completed despite of the .result() call, thus resulting in a infinite loop on futures_results. Why?

来源:https://stackoverflow.com/questions/58454576/how-to-use-python-concurrent-futures-with-decorators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!