Reputation: 241
I have main thread where I created ThreadPool. Before call function, makes initialization each thread in pool. How wait when all threads will be initialized?
Example:
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
It is necessary not to cause map_async if initialization of a thread exception occurred
Upvotes: 1
Views: 1003
Reputation: 1567
I would implement a barrier in your do_smth()
-function and skip the init_pool
. But that works only beginning with Python 3.2.
Barriers have a predefined number of parties that have to call the wait
-function of the barrier. When the barrier registers the correct number of calls it "drops" and the calling parties return to there their work simultaneously.
Something like that:
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
Upvotes: 2