gpt4 book ai didi

Python 多处理回溯(最近调用最后)

转载 作者:行者123 更新时间:2023-12-04 03:27:18 26 4
gpt4 key购买 nike

所以我尝试使用 python multiprocessing 模块制作一个简单的示例。

import time
import multiprocessing

start = time.perf_counter()

def do_something():
print('Sleeping 2 sec ...')
time.sleep(2)
print('Done Sleeping')

p1 = multiprocessing.Process(target=do_something)
p2 = multiprocessing.Process(target=do_something)

p1.start()
p2.start()

p1.join()
p2.join()

finish = time.perf_counter()

print(f'Finished in {round(finish-start, 2)} second(s)')

但是当我运行它时,打印了一个很长的错误回溯:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Goal\VSB\PDS\Project> & C:/Users/Goal/AppData/Local/Programs/Python/Python39/python.exe
c:/Goal/VSB/PDS/Project/MultiprocessingPython.py
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
exitcode = _main(fd, parent_sentinel)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 125, in _main
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
prepare(preparation_data)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 236, in prepare
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path
main_content = runpy.run_path(main_path,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path
return _run_module_code(code, init_globals, run_name,
return _run_module_code(code, init_globals, run_name,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
_run_code(code, mod_globals, init_globals,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "c:\Goal\VSB\PDS\Project\MultiprocessingPython.py", line 14, in <module>
exec(code, run_globals)
File "c:\Goal\VSB\PDS\Project\MultiprocessingPython.py", line 14, in <module>
p1.start()
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py",
line 121, in start
p1.start()
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py",
line 121, in start
self._popen = self._Popen(self)
self._popen = self._Popen(self)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py",
line 224, in _Popen
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py",
line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py",
line 327, in _Popen
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py",
line 327, in _Popen
return Popen(process_obj)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
return Popen(process_obj)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
_check_not_importing_main()
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
_check_not_importing_main()
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
raise RuntimeError('''
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.
raise RuntimeError('''
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()
...

ne 134, in _check_not_importing_main
_check_not_importing_main()
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
raise RuntimeError('''
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.
raise RuntimeError('''
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.
Finished in 0.2 second(s)
PS C:\Goal\VSB\PDS\Project> & C:/Users/Goal/AppData/Local/Programs/Python/Python39/python.exe
c:/Goal/VSB/PDS/Project/MultiprocessingPython.py
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
Traceback (most recent call last):
File "<string>", line 1, in <module>
_run_code(code, mod_globals, init_globals,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
exec(code, run_globals)
File "c:\Goal\VSB\PDS\Project\MultiprocessingPython.py", line 14, in <module>
exitcode = _main(fd, parent_sentinel)
p1.start()
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 125, in _main
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py",
line 121, in start
prepare(preparation_data)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 236, in prepare
self._popen = self._Popen(self)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py",
line 224, in _Popen
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py",
line 327, in _Popen
main_content = runpy.run_path(main_path,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path
return Popen(process_obj)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
return _run_module_code(code, init_globals, run_name,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
_run_code(code, mod_globals, init_globals,
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
_check_not_importing_main()
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
exec(code, run_globals)
File "c:\Goal\VSB\PDS\Project\MultiprocessingPython.py", line 14, in <module>
raise RuntimeError('''
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. p1.start()

File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py",
line 121, in start
self._popen = self._Popen(self)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py",
line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py",
line 327, in _Popen
return Popen(process_obj)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
_check_not_importing_main()
File "C:\Users\Goal\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
raise RuntimeError('''
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.
Finished in 0.16 second(s)
PS C:\Goal\VSB\PDS\Project>

而且这里有很多东西不知道能不能叫error?它们太多了,我什至不知道在互联网上搜索什么来解决我的问题。

最佳答案

当 Python 抛出这些大量错误语句时,请确保知道您在寻找什么 - 要注意的重要部分是错误源于 raise 调用,这意味着您需要查看在错误文本中的 raise 关键字之后。

如您所见,抛出的实际文本错误(在带有 raise 关键字的行之后找到的文本)如下:

    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.

之后,您可以轻松搜索并找到非常有用且详细的答案 here (TLDR:您需要使用 if __name__ == '__main__': 以便代码不会递归导入)。

关于Python 多处理回溯(最近调用最后),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67403393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com