3

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.

1
  • Interesting question, but I doubt this is possible. If it is, it's going to be specific to a particular implementation, e.g. CPython, since that's where the VM is implemented. See the wiki page on CPython VM internals for some details about the CPython VM.
    – Lukas Graf
    May 22, 2014 at 13:16

2 Answers 2

1

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.

8
  • You had me excited for a second =). I thought you were saying the python VM has some built-in protocol for interacting with it, but now I think you just mean that I can communicate between my two programs using sockets. Unfortunately, I want to run arbitrary code in the second VM, not a specific program. May 22, 2014 at 13:24
  • The program run in the second VM could receive your arbitrary code over a socket, and then run it and send back the results you need.
    – Dan Getz
    May 22, 2014 at 13:28
  • @Dan Getz, I agree: That is actually what I meant. You could have a pyvm.py file which contains a class which on the hand uses Pyro to connect to your main application, has a method that uses exec, and reroutes stdout/stderr back to your main application. With subprocess you start pyvm.py and once connected, You can send plain code from the main application to it.
    – galinden
    May 23, 2014 at 11:59
  • All your doing is moving the problem to a different script =). Now the arbitrary code that's being run by pyvm.py is being run in the same object space as pyvm. I'm looking for a way to run this code in its own object space. May 23, 2014 at 12:32
  • 1
    At the moment you import sys in the vm it goes into the global namespace of that python instance. It will not change anything in another python instance. That is not how it works as far as I know. I have tried it to be sure, and it really only changes the sys attr for the instance the code is executed. It even works for different versions of python which cannot possibly share the same object space. It could be that I am completely misunderstanding you, then it would be helpful if you could share some code to show what you are trying to do and which part does not work.
    – galinden
    May 23, 2014 at 20:00
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.