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

时间:2009-05-14 18:56:27

标签: python shell wxpython multiprocessing

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

修改

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

2 个答案:

答案 0 :(得分:1)

  1. 首先创建shell
  2. 通过将其本地空置
  3. 将shell与应用分离
  4. 创建代码字符串
  5. 编译代码字符串并获取代码对象
  6. 在shell中执行代码对象
  7.     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。

答案 1 :(得分:0)

您可以创建一个传递给单独进程的Queue。来自Python Docs

from multiprocessing import Process, Queue

def f(q):
    q.put([42, None, 'hello'])

if __name__ == '__main__':
    q = Queue()
    p = Process(target=f, args=(q,))
    p.start()
    print q.get()    # prints "[42, None, 'hello']"
    p.join()

示例:wx.py.shell.Shell Docs中,constructur参数以

的形式给出
__init__(self, parent, id, pos, size, style, introText, locals, 
         InterpClass, startupScript, execStartupScript, *args, **kwds) 

我没有尝试过,但locals可能是局部变量的字典,您可以将其传递给shell。所以,我会尝试以下方法:

def f(cmd_queue):
    shell = wx.py.shell.Shell(parent, id, pos, size, style, introText, locals(),
                              ...)

q = Queue()
p = Process(target=f, args=(q,))
p.start()

在shell中,您应该能够将命令放入cmd_queue,然后在父进程中读取这些命令以便执行。