无法从另一个 Python .py 文件运行进程

问题描述 投票:0回答:1

我正在创建一个类,在初始化它时,我正在调用(多进程)它的函数。当我从同一个 .py 文件创建此类时,一切正常。但是,当我从另一个 .py 文件初始化此类时 - 它失败并抛出错误......为什么?

MyDummyClass.py:

import multiprocessing
class MyDummyClass:            
    def __init__(self):        
    
        print("I am in '__init__()'")
        
        if __name__ == 'MyDummyClass':
            
            p = multiprocessing.Process(target=self.foo())
            p.start()
            
        
    def foo(self):
        print("Hello from foo()")
        

示例.py:

from MyDummyClass import MyDummyClass 
dummy = MyDummyClass()

当我运行 example.py 时,我收到此错误:

I am in '__init__()'
Hello from foo()
I am in '__init__()'
Hello from foo()

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Python\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Python\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Python\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Python\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Python\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Python\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "E:\Dropbox\Docs\Business\_NZTrackAlerts\Website\Current dev\NZtracker\cgi-bin\example1.py", line 2, in <module>
    dummy = MyDummyClass()
  File "...path to my file...\MyDummyClass.py", line 13, in __init__
    p.start()
  File "C:\Python\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Python\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Python\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Python\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Python\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\Python\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.

我无法理解(也不是来自其他帖子)如何解决它。

python django python-3.x python-multiprocessing python-multithreading
1个回答
2
投票

这与 python 实际执行脚本的方式有关。它抛出的错误本质上表明您正在尝试在 python 尚未准备好时启动一个新进程。这是因为您在主脚本中调用

dummy = MyDummyClass()
,而没有给 python 完全初始化的机会。如果您有这样的
example.py

from MyDummyClass import MyDummyClass 
if __name__ == "__main__":
    dummy = MyDummyClass()

这将产生您想要的输出:

C:\Python Scripts>python example.py
I am in '__init__()'
Hello from foo()

if __name__ == "__main__"
块告诉Python“仅当脚本作为主脚本运行时才执行此操作(即
python example.py
),这将迫使Python在运行该块之前正确初始化所有内容。

如果这个答案没有充分描述 python 后端在初始化时发生的情况,我深表歉意,我自己对此也不太了解:)

© www.soinside.com 2019 - 2024. All rights reserved.