关于在windows系统中运行python3多进程multiprocessing方法遇到RuntimeError解释及解决方法

先举个例子

import multiprocessing


def test():
	while True:
		pass


p1 = multiprocessing.Process(target=text)
p1.start
test()

代码本身没有逻辑上的毛病,但是在linux系统环境下能够运行却在windows系统环境下运行不了
下面放出在Windows系统环境下运行后所报出的错误:

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__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

可以马上看出来错误提示中有:

if __name__ == "__main":

这一段,也就是说只要在创建进程对象之前记得打上这段代码就能解决问题

至于出现freeze_support()原因:
windows环境下的python中没有直接的fork()
在Windows环境下运行此代码时需要先创建一个新的过程代码,用子进程来模拟fork()
而这一进程通过管道来到新的进程
所以需要一个判断来让程序知道是否需要执行接下来的操作

当然,错误中也提示了,可以只写

if __name__ == '__main__':

这是只在windows中存在的问题,所以可以看出学好Linux是多么的重要

作为刚入门的菜鸟程序员,我会继续将自己在学习过程中所遇到的问题写出来,既提醒了自己也希望能对其他初学者有所帮助,理解粗浅,望大家海涵

你可能感兴趣的:(pyrhon3)