2016-12-14 2 views
0

ThreadPool을 만든 주 스레드가 있습니다. 호출 기능을 사용하기 전에 풀의 각 스레드를 초기화합니다. 모든 스레드가 초기화 될 때까지 기다리는 방법은 무엇입니까?대기 풀의 모든 스레드 초기화 대기

예 : 스레드 예외의 초기화가 나는 당신의 do_smth() α- 함수에 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의 원인이 될 수 있습니다. 하지만 파이썬 3.2부터 시작됩니다.

장벽은 장벽의 wait -function이라고해야하는 미리 정의 된 수의 당사자가 있습니다. 장벽이 정확한 수의 호출을 등록하면 호출자는 "drops"하고 호출 당사자는 자신의 작업을 동시에 반환합니다. 그런

뭔가 :

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

쿨, 덕분에 꽤 솔루션입니다! 파이썬 2.7로 작성 했음에도 불구하고 당신의 대답이 나를 도왔다! 이와 같은 장벽을 시뮬레이션 할 수 있습니다. http://stackoverflow.com/a/26703365/4181533 – deniska369