process join()是如何工作的?

2024-09-27 00:21:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图理解Python中的多处理,我编写了以下程序:

from multiprocessing import Process

numOfLoops = 10

#function for each process
def func():
    a = float(0.0)
    for i in xrange(0, numOfLoops):
        a += 0.5
        print a 

processes = []
numOfProcesses = 2
#create the processes
for i in xrange(0, numOfProcesses):
    processes.append(Process(target=func))

for process in processes:
    process.start() #Start the processes
for process in processes:
    process.join()  #wait for each process to terminate  

print "shouldn't this statement be printed at the end??"

我创建了两个执行函数func()的进程。我使用join()方法等待每个进程终止,然后再继续执行该程序。这不意味着最后一个print语句应该在两个进程执行完它们的函数之后在程序的末尾打印出来吗? 但我的输出是:

^{pr2}$

这不是我所期望的。你能解释一下发生了什么事吗?在


Tags: thein程序for进程processprocessesfunc

热门问题