简体   繁体   English

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

[英]Can't run process from another python .py file

I am creating a class and when initializing it, I am calling (multiprocess) its function. 我正在创建一个类,并在初始化它时调用(多进程)它的功能。 When I am creating this class from the same .py file, everything works fine. 当我从相同的.py文件创建此类时,一切正常。 But, when I am initializing this class from another .py file - it fails and throws an error... Why? 但是,当我从另一个.py文件初始化此类时-它失败并引发错误...为什么?

MyDummyClass.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()")

example.py: example.py:

from MyDummyClass import MyDummyClass 
dummy = MyDummyClass()

When I run example.py, I am getting this error: 当我运行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.

I can't understand (and not from other posts as well) how to fix it. 我不明白(而且也没有其他帖子)如何解决它。

Thank you very much for your help!! 非常感谢您的帮助!!

This has to do with the way that python actually executes scripts. 这与python实际执行脚本的方式有关。 The error that it's throwing essentially says that you're trying to start a new process when python isn't ready yet. 实际上,它引发的错误表示您正在尝试在python还没有准备好时启动新进程。 This is because you're calling dummy = MyDummyClass() in the main script without giving python a chance to fully initialize. 这是因为您要在主脚本中调用dummy = MyDummyClass()而不给python一个完全初始化的机会。 If you instead have example.py like this: 如果您改用example.py这样的话:

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

This will produce your desired output: 这将产生您想要的输出:

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

The if __name__ == "__main__" block tells python "only execute this if the script is being run as the main script (ie python example.py ), and this will force python to initialize everything properly before it runs that block. if __name__ == "__main__"块告诉python“仅当脚本作为主脚本运行时才执行此操作(即python example.py ),这将迫使python在运行该块之前正确初始化所有内容。

Apologies if this answer doesn't describe enough what's going on in the back end of python as it's doing the initializing, I don't know a ton about it myself :) 抱歉,如果此答案未充分描述初始化过程中python后端发生的情况,我本人对此一无所知:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM