等待所有python进程完成

时间:2015-03-24 14:32:39

标签: python python-2.7 python-multiprocessing

我在一个调用这段代码的循环中启动了几个python进程:

startTimeMillis = int(round(time.time() * 1000))

for i in range(10):
        p = multiprocessing.Process(target=performCurl, args =("http://www.google.com/d", i, ))
        p.start()


endTimeMillis = int(round(time.time() * 1000))
totalTimeSeconds = (endTimeMillis - startTimeMillis)
print "The whole process took ", str(totalTimeSeconds)

我想检查所有进程完成所需的时间,那么如何让代码的最后一部分等待所有进程呢?

1 个答案:

答案 0 :(得分:7)

使用p.join()等待进程终止

all_processes = [multiprocessing.Process(...) for ... in ...]
for p in all_processes:
  p.start()

for p in all_processes:
  p.join()