我希望一个长时间运行的进程可以通过队列(或类似的东西)返回其进度,然后我将提供给进度条对话框。当进程完成时我还需要它的结果。这里的测试示例失败了,出现了RuntimeError: Queue objects should only be shared between processes through inheritance
。
import multiprocessing, time
def task(args):
count = args[0]
queue = args[1]
for i in xrange(count):
queue.put("%d mississippi" % i)
return "Done"
def main():
q = multiprocessing.Queue()
pool = multiprocessing.Pool()
result = pool.map_async(task, [(x, q) for x in range(10)])
time.sleep(1)
while not q.empty():
print q.get()
print result.get()
if __name__ == "__main__":
main()
我已经能够使用单独的Process对象使其工作(在那里我被允许传递Queue引用),但是那时候我没有池来管理我想要启动的多个进程。是否有更好的模式建议?
execnet
库http://codespeak.net/execnet/来进行多进程映射。内置的multiprocessing
还有一些问题需要修复(请参考Python追踪器)。除此之外,它的源代码相当庞大且复杂。对我来说,execnet
库看起来比multiprocessing
要好得多。 - Andrey Vlasovskikh