简体   繁体   English

多处理无法与cmd2模块一起使用,导入问题?

[英]Multiprocessing not working with cmd2 module, import issue?

Extending the solution from this question Multiprocessing code works upon import, breaks upon being called , I have injected some multiprocessing code into my project, which has promptly broken. 从这个问题扩展解决方案多处理代码在导入时起作用,在调用时中断 ,我已经将一些多处理代码注入到我的项目中,该项目立即中断了。

I think there are import issues. 我认为有进口问题。 I have two modules. 我有两个模块。 test.py looks like: test.py看起来像:

print 'omnia praeclara'
import multi4
if __name__ == "__main__":
    multi4.init_manager()
print 'rara sunt'   

and multi4.py looks like: multi4.py看起来像:

import multiprocessing as mp
def add():
    print 2+2
def init_manager():
    proc = mp.Process(target=add)
    proc.start()
    proc.join()

Now, this code works fine. 现在,此代码可以正常工作。 It produces the following when run from the command line (Windows): 从命令行(Windows)运行时,它将产生以下内容:

omnia praeclara
omnia praeclara
rara sunt
4
rara sunt

Which is to be expected (the double printing is explained in the link above). 这是可以预期的(在上面的链接中说明了双重打印)。

HOWEVER --- when I make test.py like this: 但是---当我像这样制作test.py时:

print 'omnia praeclara'
import multi4
if __name__ == "__main__":
    multi4.init_manager()
print 'rara sunt'   

import cmd2
class Prompt(cmd2.Cmd):

    def default(self, line):
        return cmd2.Cmd.default(self, line)
    prompt = '\n+++ '
    intro = '\n by that remembered or with that forgot.'
    def do_exit(self, line):
        return True

Prompt().cmdloop()

I get 我懂了

omnia praeclara
omnia praeclara
rara sunt
by that remembered or with that forgot.
+++

This is the command prompt for the cmd2 module. 这是cmd2模块的命令提示符。 The process calling add does not produce anything. 调用add的过程不会产生任何结果。 It is hung now. 现在挂了。 If I type exit into the prompt I get: 如果在提示中输入exit ,则会得到:

+++ exit
4
rara sunt

by that remembered or with that forgot.
+++

So the multiprocessing code finishes, but now I am back into my cmd2 prompt! 至此,多处理代码完成了,但是现在我回到了cmd2提示符! It is only when I exit again that I really exit. 只有当我再次退出时,我才真正退出。

Clearly, everything is being imported twice. 显然,所有内容都被导入了两次。 This was referenced in the above link, but is there any way to avoid this? 上面的链接中引用了此方法,但是有什么方法可以避免这种情况? More importantly, how can I get my processes working in the background while I am doing things in cmd2 ? 更重要的是, 当我在cmd2执行操作时如何使我的进程在后台运行?

This is the same issue that's causing the double printing - anything in test.py not protected by the if __name__ == "__main__": guard will get executed in both the parent and child processes. 这是造成的重复印刷同一个问题-在任何test.py不受保护的if __name__ == "__main__":后卫将在父进程和子进程得到执行。 You need to move the call to Prompt().cmdloop() under the guard to prevent it from being executed in the child: 您需要将调用移至保护之下的Prompt().cmdloop() ,以防止在子级中执行该调用:

print 'omnia praeclara'
import multi4

print 'rara sunt'   
import cmd2

class Prompt(cmd2.Cmd):

    def default(self, line):
        return cmd2.Cmd.default(self, line)
    prompt = '\n+++ '
    intro = '\n by that remembered or with that forgot.'
    def do_exit(self, line):
        return True

if __name__ == "__main__":
    multi4.init_manager()
    Prompt().cmdloop()

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

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