Python: connecting wx.py.shell.Shell to a separate process

I would like to create a wrapper that will manage a separate process that I created using the multiprocessing module. Possible? How?

EDIT:

I already got a way to send commands to the second process: I created code.InteractiveConsole

in this process and bound it to an input queue and an output queue, so I can control the console from my main process. But I want it in a shell, maybe wx.py.shell.Shell

so the user of the program can use it.

+1


a source to share


2 answers


  • First create a wrapper
  • Disconnect the shell from your application by resetting its local resources.
  • Create your line of code
  • Compile a line of code and get a code object
  • Execute a code object in a 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)



Note: The Str I code stolen from the first poster may not work here due to some etching issues. But the point is, you can execute your own Str code remotely in a shell.

+1


a source


You can create Queue

which you will pass to a separate process. From the 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()

      

EXAMPLE: In wx.py.shell.Shell Docs , constructor parameters are specified as

__init__(self, parent, id, pos, size, style, introText, locals, 
         InterpClass, startupScript, execStartupScript, *args, **kwds) 

      



I haven't tried it, but it locals

might be a dictionary of local variables that you can pass to the shell. So, I would try the following:

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()

      

Inside the shell, you will need to put your commands in cmd_queue

, which will then need to be read by the parent process to be executed.

0


a source







All Articles