|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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() 用进程控制,就成了这样。。而且没有成功启动。。
本帖最后由 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()
复制代码
|
|