无法 运行 从另一个 python .py 文件处理
Can't run process from another python .py file
我正在创建一个 class 并在初始化它时调用(多进程)它的函数。当我从同一个 .py 文件创建这个 class 时,一切正常。但是,当我从另一个 .py 文件初始化此 class 时 - 它失败并抛出错误...为什么?
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:
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实际执行脚本的方式有关。它抛出的错误本质上是在 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 后端在进行初始化时发生的事情,我自己对此并不了解 :)
我正在创建一个 class 并在初始化它时调用(多进程)它的函数。当我从同一个 .py 文件创建这个 class 时,一切正常。但是,当我从另一个 .py 文件初始化此 class 时 - 它失败并抛出错误...为什么?
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:
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实际执行脚本的方式有关。它抛出的错误本质上是在 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 后端在进行初始化时发生的事情,我自己对此并不了解 :)