多处理代码在导入时起作用,在被调用时中断 [英] Multiprocessing code works upon import, breaks upon being called

查看:176
本文介绍了多处理代码在导入时起作用,在被调用时中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在名为 test.py 的文件中,我有

print 'i am cow'
import multi4
print 'i am cowboy'

并在 multi4.py 我有

import multiprocessing as mp
manager = mp.Manager()
print manager

我很困惑的方式这段代码运作。

I am confused by the way this code operates.

在命令行中,如果我输入 python ,然后在python环境中键入 import test.py 我得到了预期的行为:

At the command line, if I type python and then in the python environment if I type import test.py I get the expected behavior:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.

>>>import test
i am cow
<multiprocessing.managers.SyncManager object at 0x025209B0>
i am cowboy 
>>>

但是如果我输入 test.py at命令行,我得到了

However if I type test.py at the command line, I get

i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow
i am cow

除非我杀了它,否则大概会继续下去。当我杀了它时,我得到了一堆重复的错误:

Which would presumably go on forever unless I kill it. When I kill it, I get a bunch of repeated errors:

KeyboardInterrupt
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\multiprocessing\forking.py", line 373, in main
    prepare(preparation_data)
  File "C:\Python27\lib\multiprocessing\forking.py", line 488, in prepare
    '__parents_main__', file, path_name, etc
KeyboardInterrupt
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\multiprocessing\forking.py", line 373, in main
    prepare(preparation_data)
  File "C:\Python27\lib\multiprocessing\forking.py", line 488, in prepare
    '__parents_main__', file, path_name, etc
KeyboardInterrupt

那是怎么回事?为什么它在导入时表现一种方式,另一种在我尝试运行时呢?

So what is going on? Why does it behave one way under import, and another when I try to run it?

推荐答案

多处理 将无法正常工作,因为它无法正常运行在它生成的子进程中重新导入 __ main __ 。然而,实际上实际上帮助你,因为它保持 manager = mp.Manager()行在子进程中递归执行当 Manager 启动时产生。

multiprocessing won't work properly in the interactive prompt on Windows, because it can't properly re-import __main__ in the child processes it spawns. However, that actually actually helps you here, since it keeps the manager = mp.Manager() line from being recursively executed in the child process that spawns when the Manager starts up.

但是在实际的脚本中,孩子可以重新导入 __ main __ 正确。您正在看到无限递归,因为如果__name__ ==__ main__,您不会使用保护对 mp.Manager()的调用: guard,在Windows上需要到阻止 mp.Manager()在重新导入时在子代中执行:

In the actual script, however, the child can re-import __main__ properly. You're seeing the infinite recursion because you're not protecting the call to mp.Manager() with an if __name__ == "__main__": guard, which is required on Windows to prevent mp.Manager() from being executed in the child when the re-import occurs:

import multiprocessing as mp
if __name__ == "__main__":
    manager = mp.Manager()
    print manager

编辑:

您的示例,您的主要脚本( test.py)导入创建 Manager 的模块,需要一些重构。您需要通过调用实际使用 multiprocessing 的模块中的方法,从主脚本实例化 Manager

Your example, where your main script (test.py) imports the module that creates the Manager, needs a bit of refactoring. You need to instantiate the Manager from the main script, by calling a method in the module that actually uses multiprocessing:

print 'i am cow'
import multi4
if __name__ == "__main__":
    multi4.init_manager()
print 'i am cowboy'

multi4。 py

import multiprocessing as mp
manager = None
def init_manager():
    global manager
    manager = mp.Manager()

这是唯一的方法确保在实际执行脚本时只创建 Manager

This is the only way to make sure you only create the Manager when you're actually executing a script.

这篇关于多处理代码在导入时起作用,在被调用时中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆