windows系统中没有unix/linux中的fork函数来提供多进程功能,但是在Python中提供了multiprocessing模块来完成多进程功能。在所有的示例代码中,都出现了if __name__=="__main__"的判断语句来正确完成多进程功能,那么这个判断语句究竟起到了什么作用。
先看一下在不使用这个判断语句时执行程序时会出现什么结果。
import os
from multiprocessing import Process
def child_process(name):
print('i am child process %s(%s)' %(name,os.getpid()))
p = Process(target=child_process,args=('test',))
p.start()
p.join()
上面这段代码使用了Process这个类来创建了子进程而且没有使用if __name__=="__main__",执行后的结果如下
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ =