由于某种原因,这个python多处理代码不起作用



所以,我刚开始使用Python多处理。我尝试了这个例子,但没有完全奏效:

import multiprocessing
def function():
time.sleep(1)
print("slept once")
p1 = multiprocessing.Process(target=function)
p2 = multiprocessing.Process(target=function)
p1.start()
p2.start()

它应该输出这个:

(sleeping 1 second)
slept once
slept once

但它却给了我一个错误:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 114, in _main
prepare(preparation_data)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
File "C:UserskristAppDataLocalProgramsPythonPython36librunpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:UserskristAppDataLocalProgramsPythonPython36librunpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:UserskristAppDataLocalProgramsPythonPython36librunpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:UserskristPycharmProjectschat_appclient_1.py", line 11, in <module>
p1.start()
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingprocess.py", line 105, in start
self._popen = self._Popen(self)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingcontext.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingcontext.py", line 322, in _Popen
return Popen(process_obj)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingpopen_spawn_win32.py", line 33, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 143, in get_preparation_data
_check_not_importing_main()
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.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.
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 114, in _main
prepare(preparation_data)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
File "C:UserskristAppDataLocalProgramsPythonPython36librunpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:UserskristAppDataLocalProgramsPythonPython36librunpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:UserskristAppDataLocalProgramsPythonPython36librunpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:UserskristPycharmProjectschat_appclient_1.py", line 11, in <module>
p1.start()
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingprocess.py", line 105, in start
self._popen = self._Popen(self)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingcontext.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingcontext.py", line 322, in _Popen
return Popen(process_obj)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingpopen_spawn_win32.py", line 33, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 143, in get_preparation_data
_check_not_importing_main()
File "C:UserskristAppDataLocalProgramsPythonPython36libmultiprocessingspawn.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.

我使用的是windows 11,python 3.6

希望有人能帮忙!

这里只是一堆随机的东西,因为stackoverflow不允许我发布以下内容:

数据:1,myfile;1:0_sub_.error:help.error:help.data:1,myfile;1:0_sub_错误:help.data:1,myfile;1:0_sub_error:help.data:1,myfile;1:0_sub_error:help.data:1,myfile;1:0_sub_;;000令吉;ip/ip.error:help.data:1,myfile;1:0_ sub_

如果您添加了预期的习惯用法:,您的代码运行良好

import time
import multiprocessing
def function():
time.sleep(1)
print("slept once")
if __name__ == '__main__':
p1 = multiprocessing.Process(target=function)
p2 = multiprocessing.Process(target=function)
p1.start()
p2.start()

除非遵循多处理库的指导原则[1],否则您无法启动新进程。

这个库中有一个特定的指导原则,说明start((方法应该如何仅在if __name__ == '__main__条件中运行。

为什么?因为在文件直接运行而不导入的情况下生成新进程更安全。

因此,这应该起作用:

import multiprocessing
import time

def function():
time.sleep(1)
print("slept once")
p1 = multiprocessing.Process(target=function)
p2 = multiprocessing.Process(target=function)
if __name__ == '__main__':
p1.start()
p2.start()

[0]如何使用MultiProcessing库?https://docs.python.org/3/library/multiprocessing.html

[1]https://docs.python.org/3/library/multiprocessing.html#multiprocessing-编程

相关内容

  • 没有找到相关文章

最新更新