2016-12-14 7 views
0

私はThreadPoolを作成したメインスレッドを持っています。コール関数の前にプール内の各スレッドを初期化します。すべてのスレッドが初期化されるまでの待ち時間プール内のすべてのスレッドの初期化待機

例:スレッドの例外の初期化は、私はあなたのdo_smth() -functionでbarrierを実装し、init_poolをスキップするでしょう

+0

私は、この目的のために、マルチプロセッシングから値を使用することができますが、それは – deniska369

答えて

1

を発生した場合

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を起こさないようにする必要があります。しかし、これは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 
+0

クール、おかげでかなり解決策ではありません!私がPython 2.7で書いたという事実にもかかわらず、あなたの答えは私を助けました! 私はこのような障壁をシミュレートできます http://stackoverflow.com/a/26703365/4181533 – deniska369

関連する問題