我正在阅读有关Python中多处理模块的各种教程,并且无法理解为何/何时调用process.join().例如,我偶然发现了这个例子:
nums = range(100000)
nprocs = 4
def worker(nums, out_q):
""" The worker function, invoked in a process. 'nums' is a
list of numbers to factor. The results are placed in
a dictionary that's pushed to a queue.
"""
outdict = {}
for n in nums:
outdict[n] = factorize_naive(n)
out_q.put(outdict)
# Each process will get 'chunksize' nums and a queue to put his out
# dict into
out_q = Queue()
chunksize = int(math.ceil(len(nums) / float(nprocs)))
procs = []
for i in range(nprocs):
p = multiprocessing.Process(
target=worker,
args=(nums[chunksize * i:chunksize * (i + 1)],
out_q))
procs.append(p)
p.start()
# Collect all results into a single result dict. We know how many dicts
# with results to expect.
resultdict = {}
for i in range(nprocs):
resultdict.update(out_q.get())
# Wait for all worker processes to finish
for p in procs:
p.join()
print resultdict
根据我的理解,process.join()将阻止调用进程,直到调用其join方法的进程已完成执行.我还相信在上面的代码示例中启动的子进程在完成目标函数时完成执行,也就是说,在将结果推送到out_q之后.最后,我相信out_q.get()会阻止调用进程,直到有结果被拉出.因此,如果您考虑代码:
resultdict = {}
for i in range(nprocs):
resultdict.update(out_q.get())
# Wait for all worker processes to finish
for p in procs:
p.join()
out_q.get()调用阻止主进程,直到每个工作进程完成将其结果推送到队列.因此,当主进程退出for循环时,每个子进程应该已经完成??执行,对吗?
如果是这种情况,是否有任何理由在此时调用p.join()方法?并非所有工作进程都已完成,那么这是如何导致主进程“等待所有工作进程完成?”我问的主要是因为我在多个不同的例子中看到了这一点,如果我不理解某些东西,我很好奇.