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

时间:2021-05-05 14:32:39

标签: python python-multiprocessing

所以我尝试使用 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>

而且这里有很多东西我不知道它们是否可以称为错误?它们太多了,我什至不知道在互联网上搜索什么来解决我的问题。

1 个答案:

答案 0 :(得分:3)

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

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

raise

之后,您可以轻松搜索并找到非常有用和详细的答案 here(TLDR:您需要使用 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. 以便代码不会递归导入)。

相关问题