我想并行运行一堆作业,然后在所有作业完成后继续。我有类似的东西
# based on example code from https://pymotw.com/2/multiprocessing/basics.html
import multiprocessing
import random
import time
def worker(num):
"""A job that runs for a random amount of time between 5 and 10 seconds."""
time.sleep(random.randrange(5,11))
print('Worker:' + str(num) + ' finished')
return
if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
jobs.append(p)
p.start()
# Iterate through the list of jobs and remove one that are finished, checking every second.
while len(jobs) > 0:
jobs = [job for job in jobs if job.is_alive()]
time.sleep(1)
print('*** All jobs finished ***')
它可以工作,但是我敢肯定,有一种等待所有工作完成的更好的方法,而不是一次又一次地遍历它们直到完成。
最佳答案
关于什么?
for job in jobs:
job.join()
这将阻塞,直到第一个过程完成,然后再进行下一个,依此类推。进一步了解
join()
关于python - 等待所有多处理作业完成后再继续,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33933561/