启动进程报错 RuntimeError: An attempt has been made to start a new process before the current process

朱雀 2020-10-28 11:22 1841阅读 0赞

报错内容如下:

  1. RuntimeError:
  2. An attempt has been made to start a new process before the
  3. current process has finished its bootstrapping phase.
  4. This probably means that you are not using fork to start your
  5. child processes and you have forgotten to use the proper idiom
  6. in the main module:
  7. if \_\_name\_\_ == '\_\_main\_\_':
  8. freeze\_support()
  9. ...
  10. The "freeze\_support()" line can be omitted if the program
  11. is not going to be frozen to produce an executable.

根据报错提示可知:

没有在模块中使用

if __name__ == ‘__main__‘:
freeze_support()

如果程序需要,可以省略“freeze_support()”行

更改代码如下:可以正常运行

  1. # 进程
  2. from multiprocessing import Process
  3. def foo(i):
  4. print(" This is Process ", i)
  5. def main():
  6. for i in range(5):
  7. p=Process(target=foo, args=(i, ))
  8. p.start()
  9. if __name__ == '__main__':
  10. main()

输出结果:

  1. This is Process 1
  2. This is Process 0
  3. This is Process 2
  4. This is Process 3
  5. This is Process 4

发表评论

表情:
评论列表 (有 0 条评论,1841人围观)

还没有评论,来说两句吧...

相关阅读