为什么在需要结果后调用 join() 过程?

Why call join() process after results are needed?

我在某处看到过此代码,但无法理解它如何才能正常工作:

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())

time.sleep(5)

# Wait for all worker processes to finish
for p in procs:
    p.join()

print resultdict

time.sleep(15)

在我看来,在查询队列的输出之前等待所有进程终止是有意义的。如何确定在启动所有进程后立即查询 Queue 时,Queue 将包含所有输出? (即,如果工作人员完成所需的时间比启动所有进程然后开始查看队列所需的时间相对长,会发生什么情况)

另一个稍微相关的问题:Python 文档说 "A process can be joined many times." 为什么要多次加入该过程?如果它已经终止,检查它是否再次终止的目的是什么?

It seems to me that it would make sense to wait for all processes to terminate before querying the Queue for their output.

是的,它是这样工作的。

How can one be certain that, in querying the Queue immediately after starting all processes, the Queue will contain all the outputs?

它将等到最后一个进程工作。

Why would one want to join the process multiple times?

有时我们需要 运行 一个过程更多次,比如如果我们想用其他或相同的参数一次又一次地更新一个变量,并且说我们有很多时间直到 "slowest" 流程未完成。