等待池中所有线程的初始化

时间:2016-12-14 12:11:07

标签: python multiprocessing threadpool

我有创建ThreadPool的主线程。在调用函数之前,在池中初始化每个线程。所有线程初始化时如何等待?

示例:

from multiprocessing.pool import ThreadPool

def main():
    # in main thread
    pool = ThreadPool(processes=5, initializer=init_pool)

    # >>> here it is I want to wait for initialized

    pool.map_async(do_smth).get()

 def init_pool():
    # initialized new thread in pool
    pass

def do_smth():
    # do somethings
    pass

如果发生线程异常的初始化,则不必导致map_async

1 个答案:

答案 0 :(得分:1)

我会在do_smth() - 函数中实现barrier并跳过init_pool。但这只能从Python 3.2开始。

障碍有预定数量的参与者必须调用屏障的wait - 功能。当障碍物记录正确的呼叫数量时,它会掉线"并且主叫方同时返回他们的工作。

类似的东西:

from multiprocessing.pool import ThreadPool
from threading import Barrier

b = Barrier(parties=5)

def main():
    # in main thread
    pool = ThreadPool(processes=5)

    pool.map_async(do_smth).get()

def do_smth():
    #initialize here
    b.wait() #call to the barrier
    # do somethings
    pass