等待池中所有线程的初始化
wait initialized of all threads in pool
我在主线程中创建了 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
我会在你的 do_smth()
函数中实现 barrier 并跳过 init_pool
。但这只适用于 Python 3.2.
障碍有预定义数量的各方必须调用障碍的 wait
函数。当屏障注册正确数量的呼叫时,它 "drops" 和呼叫方 return 会同时在那里工作。
类似的东西:
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
我在主线程中创建了 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
我会在你的 do_smth()
函数中实现 barrier 并跳过 init_pool
。但这只适用于 Python 3.2.
障碍有预定义数量的各方必须调用障碍的 wait
函数。当屏障注册正确数量的呼叫时,它 "drops" 和呼叫方 return 会同时在那里工作。
类似的东西:
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