当前位置: 代码迷 >> 综合 >> python高级进阶_15_解决多进程报错为“ in _check_not_importing_main is not going to be frozen to produce an”
  详细解决方案

python高级进阶_15_解决多进程报错为“ in _check_not_importing_main is not going to be frozen to produce an”

热度:83   发布时间:2023-10-24 20:16:24.0

原始代码如下:

from multiprocessing  import Process
import time
def test():while True:print("------子进程---------")time.sleep(3)p = Process(target=test)
p.start()
while True:print("------------main------------")time.sleep(3)

报错为:
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 yourchild processes and you have forgotten to use the proper idiomin the main module:if __name__ == '__main__':freeze_support()...根据提示信息说的是, 你既没有用fork (linux 和mac 支持),也没有 用proper idiom  ,所以要报错,

它很智能给你了解决方法, 加上 if __name__ == ‘__main__’:

修改后 就不报错了 如下;

from multiprocessing  import Process
import time
def test():while True:print("------子进程---------")time.sleep(3)
def main():while True:print("------------main------------")time.sleep(3)if __name__ == '__main__':p = Process(target=test)p.start()main()