进程-join()如何工作?
作者:互联网
我试图理解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()方法等待每个进程终止,然后再继续执行该程序.这是否意味着在两个进程执行完它们的功能之后,最后的打印语句应该在程序的末尾打印?
但是我的输出是:
shouldn't this statement be printed at the end??
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
这不是我所期望的.你能解释发生了什么吗?
解决方法:
这很简单,它只等待每个正在运行的进程完成,然后再返回.
标签:multiprocessing,multiprocess,python,process,python-multiprocessing 来源: https://codeday.me/bug/20191119/2033218.html