我想创建一个 shell 来控制我使用多处理模块创建的单独进程。可能的?怎么办?
编辑:
我已经实现了一种向辅助进程发送命令的方法:我在该进程中创建了一个 code.InteractiveConsole
,并将其附加到输入队列和输出队列,这样我就可以命令我的主进程的控制台。但我希望它在 shell 中,可能是 wx.py.shell.Shell
,这样程序的用户就可以使用它。
最佳答案
- 首先创建 shell
- 通过将本地变量清空来将 shell 与您的应用分离
- 创建您的代码字符串
- 编译代码字符串并获取代码对象
- 在 shell 中执行代码对象
from wx.py.shell import Shell frm = wx.Frame(None) sh = Shell(frm) frm.Show() sh.interp.locals = {} codeStr = """ from multiprocessing import Process, Queue def f(q): q.put([42, None, 'hello']) q = Queue() p = Process(target=f, args=(q,)) p.start() print q.get() # prints "[42, None, 'hello']" p.join() """ code = compile(codeStr, '', 'exec') sh.interp.runcode(code)
注意: 由于一些酸洗问题,我从第一张海报中窃取的 codeStr 可能无法在这里工作。但重点是您可以在 shell 中远程执行您自己的 codeStr。
关于Python:将 wx.py.shell.Shell 插入单独的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/865082/