鱼C论坛

 找回密码
 立即注册
查看: 1299|回复: 4

[已解决]python多进程报错

[复制链接]
发表于 2022-4-23 21:28:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
刚开始学 Python 的多任务,正在学多进程,可以下代码一允许,就有报错

  1. # encoding=utf-8

  2. import multiprocessing
  3. import time

  4. def dance():
  5.     for i in range(3):
  6.         print("跳舞~~~")
  7.         time.sleep(0.2)

  8. def sing():
  9.     for i in range(3):
  10.         print("唱歌~~~")
  11.         time.sleep(0.2)

  12. dance_process = multiprocessing.Process(target=dance)

  13. dance_process.start()

  14. sing()
复制代码



然后控制台就出现了如下报错,百度没查到原因。。


  1. E:\Python\pypath\Python\Python37\python.exe E:/Python/output/project01/src/test/test54.py
  2. 唱歌~~~
  3. Traceback (most recent call last):
  4.   File "<string>", line 1, in <module>
  5.   File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
  6.     exitcode = _main(fd)
  7.   File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 114, in _main
  8.     prepare(preparation_data)
  9.   File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 225, in prepare
  10.     _fixup_main_from_path(data['init_main_from_path'])
  11.   File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
  12.     run_name="__mp_main__")
  13.   File "E:\Python\pypath\Python\Python37\lib\runpy.py", line 263, in run_path
  14.     pkg_name=pkg_name, script_name=fname)
  15.   File "E:\Python\pypath\Python\Python37\lib\runpy.py", line 96, in _run_module_code
  16.     mod_name, mod_spec, pkg_name, script_name)
  17.   File "E:\Python\pypath\Python\Python37\lib\runpy.py", line 85, in _run_code
  18.     exec(code, run_globals)
  19.   File "E:\Python\output\project01\src\test\test54.py", line 18, in <module>
  20.     dance_process.start()
  21.   File "E:\Python\pypath\Python\Python37\lib\multiprocessing\process.py", line 112, in start
  22.     self._popen = self._Popen(self)
  23.   File "E:\Python\pypath\Python\Python37\lib\multiprocessing\context.py", line 223, in _Popen
  24.     return _default_context.get_context().Process._Popen(process_obj)
  25.   File "E:\Python\pypath\Python\Python37\lib\multiprocessing\context.py", line 322, in _Popen
  26.     return Popen(process_obj)
  27.   File "E:\Python\pypath\Python\Python37\lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
  28.     prep_data = spawn.get_preparation_data(process_obj._name)
  29.   File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
  30.     _check_not_importing_main()
  31.   File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
  32.     is not going to be frozen to produce an executable.''')
  33. RuntimeError:
  34.         An attempt has been made to start a new process before the
  35.         current process has finished its bootstrapping phase.

  36.         This probably means that you are not using fork to start your
  37.         child processes and you have forgotten to use the proper idiom
  38.         in the main module:

  39.             if __name__ == '__main__':
  40.                 freeze_support()
  41.                 ...

  42.         The "freeze_support()" line can be omitted if the program
  43.         is not going to be frozen to produce an executable.
  44. 唱歌~~~
  45. 唱歌~~~

  46. Process finished with exit code 0
复制代码


可以看到,sing() 方法没有用进程来控制,是正常的,但是 dance() 用进程控制,就成了这样。。而且没有成功启动。。
最佳答案
2022-4-23 21:30:56
本帖最后由 isdkz 于 2022-4-23 21:40 编辑

报错提示中都告诉你了
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.
  1. # encoding=utf-8

  2. import multiprocessing
  3. import time

  4. def dance():
  5.     for i in range(3):
  6.         print("跳舞~~~")
  7.         time.sleep(0.2)

  8. def sing():
  9.     for i in range(3):
  10.         print("唱歌~~~")
  11.         time.sleep(0.2)

  12. if __name__ == '__main__':                                     # 多进程要加上这个
  13.     dance_process = multiprocessing.Process(target=dance)

  14.     dance_process.start()

  15.     sing()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-23 21:30:56 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2022-4-23 21:40 编辑

报错提示中都告诉你了
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.
  1. # encoding=utf-8

  2. import multiprocessing
  3. import time

  4. def dance():
  5.     for i in range(3):
  6.         print("跳舞~~~")
  7.         time.sleep(0.2)

  8. def sing():
  9.     for i in range(3):
  10.         print("唱歌~~~")
  11.         time.sleep(0.2)

  12. if __name__ == '__main__':                                     # 多进程要加上这个
  13.     dance_process = multiprocessing.Process(target=dance)

  14.     dance_process.start()

  15.     sing()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-23 21:42:11 | 显示全部楼层
isdkz 发表于 2022-4-23 21:30
报错提示中都告诉你了

还真是这个原因。。报错里愣是没看懂这里的意思。。以为是多进程包里的 main 方法提示。。

感谢指点
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-23 21:43:15 | 显示全部楼层
foxiangzun 发表于 2022-4-23 21:42
还真是这个原因。。报错里愣是没看懂这里的意思。。以为是多进程包里的 main 方法提示。。

...

你可以复制到翻译软件看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-23 21:45:13 | 显示全部楼层
isdkz 发表于 2022-4-23 21:43
你可以复制到翻译软件看看

这一块上面的扔到了翻译软件里,下面也扔进了翻译软件里,唯独这一部分给忽略了。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 04:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表