Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python multithreading 等待Executor完成,而任务可以提交更多任务_Python Multithreading_Python 3.6_Threadpoolexecutor - Fatal编程技术网

Python multithreading 等待Executor完成,而任务可以提交更多任务

Python multithreading 等待Executor完成,而任务可以提交更多任务,python-multithreading,python-3.6,threadpoolexecutor,Python Multithreading,Python 3.6,Threadpoolexecutor,我将任务提交给ThreadPoolExecutor,而这些任务在某些情况下可以提交更多任务。我想等到所有的任务都完成。我有一个解决方案,在执行器的内部队列大小上使用条件,但尽管它有效,我觉得这不是最好的方法 下面是我正在做的一个例子,我把它简化为一个非常简单的例子: from concurrent.futures.thread import ThreadPoolExecutor from threading import Condition def func(e: ThreadPoolExe

我将任务提交给ThreadPoolExecutor,而这些任务在某些情况下可以提交更多任务。我想等到所有的任务都完成。我有一个解决方案,在
执行器的内部队列大小上使用
条件
,但尽管它有效,我觉得这不是最好的方法

下面是我正在做的一个例子,我把它简化为一个非常简单的例子:

from concurrent.futures.thread import ThreadPoolExecutor
from threading import Condition


def func(e: ThreadPoolExecutor, c: Condition, val: int):
    print(val)
    if val < 5:
        # A case when I need to submit a new task
        e.submit(func, e, c, val + 1)
    with c:
        c.notify()


with ThreadPoolExecutor(max_workers=5) as e:
    c = Condition()
    for i in range(10):
        print(e._work_queue.qsize())
        e.submit(func, e, c, 1)
    with c:
        while not e._work_queue.qsize() == 0:
            c.wait_for(lambda: e._work_queue.qsize() == 0)
从concurrent.futures.thread导入ThreadPoolExecutor
从线程导入条件
def func(e:ThreadPoolExecutor,c:Condition,val:int):
打印(val)
如果val<5:
#当我需要提交新任务时的案例
e、 提交(func、e、c、val+1)
用c:
c、 通知()
将ThreadPoolExecutor(最大工作线程数=5)设置为e:
c=条件()
对于范围(10)内的i:
打印(e._work_queue.qsize())
e、 提交(func、e、c、1)
用c:
而不是e._work_queue.qsize()==0:
c、 等待(lambda:e._work_queue.qsize()==0)
有没有更好和/或更干净的方法