我想使用多处理来提高我的项目的速度。
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())
这里的问题是,如果一个进程完成构建它的东西,它将等到其他进程完成,而我希望这个进程做其他事情。
我怎样才能做到这一点?我想我可以使用一组工人,但我真的不明白如何使用它,因为我需要检索结果。有人可以帮忙吗?