我在我的项目中使用多处理。我有一个将结果放入队列的工作函数。一切正常。但随着x的大小增加(在我的情况下,x是一个数组)出了问题。这里是我的代码的简化版本:Python中的多处理被阻止
def do_work(queue, x):
result = heavy_computation_function(x)
queue.put(result) # PROBLEM HERE
def parallel_something():
queue = Queue()
procs = [Process(target=do_work, args=i) for i in xrange(20)]
for p in procs: p.start()
for p in procs: p.join()
results = []
while not queue.empty():
results.append(queue.get)
return results
我在系统中看到监控蟒蛇流程工作,但随后事情发生,所有进程都在运行,但什么都不做。这是我输入ctrl-D时得到的结果。
pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
我做了一些测试。如果我不把结果放在一切正常的地方,那么这个问题看起来好像是把结果放在队列中,但那样就没有用处。
您似乎从不将队列对象传递给新进程。 'Process'的'args'应该是'tuple'。尝试将其更改为'args =(queue,i)'。你的'queue.get'也需要一些括号,以便它成为'queue.get()'。 – Wessie