マルチプロセッシングを使用してプロジェクトの速度を上げたいです。
from multiprocessing import Queue, Process
def build(something):
# ... Build something ...
return something
# Things I want to build.
# Each of these things requires DIFFERENT TIME to be built.
some_things = [a_house, a_rocket, a_car]
#________________________________
# My approach
def do_work(queue, func, args):
queue.put(func(*args))
# Initialize a result queue
queue = Queue()
# Here I'll need to distribute the tasks (in case there are many)
# through each process. For example process 1 build a house and a rocket
# and so on. Anyway this is not the case..
procs = [Process(target=do_work, args=thing) for thing in some_things]
# Finally, Retrieve things from the queue
results = []
while not queue.empty():
results.append(queue.get())
ここでの問題は、プロセスがそのようなものを構築するために終了した場合、他のプロセスが終了するまで待機し、そのようなプロセスに何か他のことをさせたいということです。
どうすればこれを達成できますか?ワーカーのプールを使用できると思いますが、結果を取得する必要があるため、使用方法がよくわかりません。誰かがこれを手伝ってくれる?