我试图理解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}$这不是我所期望的。你能解释一下发生了什么事吗?在
它非常简单,它只需等待每个正在运行的进程完成,当发生这种情况时,它返回。在
之所以称为
join
,是因为它将进程合并为一个进程。相关问题 更多 >
编程相关推荐