Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically launch and interact with python virtual machine

Does anyone know how to launch a new python virtual machine from inside a python script, and then interact with it to execute code in a completely separate object space? In addition to code execution, I'd like to be able to access the objects and namespace on this virtual machine, look at exception information, etc.

I'm looking for something similar to python's InteractiveInterpreter (in the code module), but as far as I've been able to see, even if you provide a separate namespace for the interpreter to run in (through the locals parameter), it still shares the same object space with the script that launched it. For instance, if I change an attribute of the sys module from inside InteractiveInterpreter, the change takes affect in the script as well. I want to completely isolate the two, just like if I was running two different instances of the python interpreter to run two different scripts on the same machine.

I know I can use subprocess to actually launch python in a separate process, but I haven't found any good way to interact with it the way I want. I imagine I could probably invoke it with '-i' and push code to it through it's stdin stream, but I don't think I can get access to its objects at all.

like image 728
brianmearns Avatar asked Apr 19 '26 09:04

brianmearns


2 Answers

If you start a new Python instance with subprocess, afterwards you can communicate through sockets: to avoid doing the low level stuff you could have a look at the twisted framework, also have a look at Pyro http://pythonhosted.org/Pyro4/. Personally I find Pyro a bit easier to get started.

like image 198
galinden Avatar answered Apr 20 '26 22:04

galinden


It may depend on Python implementation such as Pypy, Jython. In CPython, you have to use a separate process if you want an independent interpreter otherwise at the very least GIL is shared.

multiprocessing, concurrent.futures modules allow you to run arbitrary Python code in separate processes and to communicate with the parent easily.

like image 23
Denis Patrikeev Avatar answered Apr 20 '26 21:04

Denis Patrikeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!