所以我希望使用 python 多处理模块创建一个进程,我希望它成为更大脚本的一部分。 (我还想从中得到很多其他东西,但现在我会满足于此)
我从以下位置复制了最基本的代码多处理文档 https://docs.python.org/3.6/library/multiprocessing.html并稍作修改
然而,一切之外的if __name__ == '__main__':
语句得到repeated每次调用 p.join() 时。
这是我的code:
from multiprocessing import Process
data = 'The Data'
print(data)
# worker function definition
def f(p_num):
print('Doing Process: {}'.format(p_num))
print('start of name == main ')
if __name__ == '__main__':
print('Creating process')
p = Process(target=f, args=(data,))
print('Process made')
p.start()
print('process started')
p.join()
print('process joined')
print('script finished')
这就是我expected:
The Data
start of name == main
Creating process
Process made
process started
Doing Process: The Data
process joined
script finished
Process finished with exit code 0
这是reality:
The Data
start of name == main
Creating process
Process made
process started
The Data <- wrongly repeated line
start of name == main <- wrongly repeated line
script finished <- wrongly executed early line
Doing Process: The Data
process joined
script finished
Process finished with exit code 0
我不确定这是否是由于if
声明或p.join()
或其他东西以及扩展为什么会发生这种情况。有人可以解释一下吗what造成了这个和why?
为了清楚起见,因为有些人无法复制我的问题,但我有;我正在使用 Windows Server 2012 R2 Datacenter,并且我正在使用 python 3.5.3。