鱼C论坛

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

[已解决]python多进程报错

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

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

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

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

import multiprocessing
import time

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

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

dance_process = multiprocessing.Process(target=dance)

dance_process.start()

sing()


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

E:\Python\pypath\Python\Python37\python.exe E:/Python/output/project01/src/test/test54.py
唱歌~~~
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "E:\Python\pypath\Python\Python37\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "E:\Python\pypath\Python\Python37\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "E:\Python\pypath\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "E:\Python\output\project01\src\test\test54.py", line 18, in <module>
    dance_process.start()
  File "E:\Python\pypath\Python\Python37\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "E:\Python\pypath\Python\Python37\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "E:\Python\pypath\Python\Python37\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "E:\Python\pypath\Python\Python37\lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "E:\Python\pypath\Python\Python37\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
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.
唱歌~~~
唱歌~~~

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.
# encoding=utf-8

import multiprocessing
import time

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

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

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

    dance_process.start()

    sing()
想知道小甲鱼最近在做啥?请访问 -> 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.
# encoding=utf-8

import multiprocessing
import time

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

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

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

    dance_process.start()

    sing()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

感谢指点
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

...

你可以复制到翻译软件看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

这一块上面的扔到了翻译软件里,下面也扔进了翻译软件里,唯独这一部分给忽略了。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-18 16:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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