Python:将 wx.py.shell.Shell 插入单独的进程

标签 python shell wxpython multiprocessing

我想创建一个 shell 来控制我使用多处理模块创建的单独进程。可能的?怎么办?

编辑:

我已经实现了一种向辅助进程发送命令的方法:我在该进程中创建了一个 code.InteractiveConsole ,并将其附加到输入队列和输出队列,这样我就可以命令我的主进程的控制台。但我希望它在 shell 中,可能是 wx.py.shell.Shell,这样程序的用户就可以使用它。

最佳答案

  1. 首先创建 shell
  2. 通过将本地变量清空来将 shell 与您的应用分离
  3. 创建您的代码字符串
  4. 编译代码字符串并获取代码对象
  5. 在 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/

相关文章:

python - 如何让 PyC​​harm 在其控制台中显示 unicode 数据?

bash - 在 bash 中,如何设置变量来包含可变数量的命令行参数?

python - 如何在wxpython中制作 Canvas (矩形)?

python - wxpython 中可调整大小的分隔线?

coding-style - wx.Frame样式位掩码中使用的所有样式参数的列表

python - Sphinx 'latin-1' 编解码器无法编码字符' - 想要使用 utf-8

python - 为什么 ActivePython 存在?

python - 使用 python 从大型 json 文件加载元素

linux - 使用 TCL 驱动 linux shell

shell - 如何使用命令或 shell 脚本从照片中剪切透明部分