views:

364

answers:

4

I am working on embedding python in to c++. In some peculiar case I require two separate instances of the interpreter in same thread.

Can I wrap Python interpreter in to a c++ class and get services from two or more class instances?

+2  A: 

You can, but I'd recommend you not to re-implement a Python interpreter when there is a standard implementation. Use boost::python to interface with Python.

Eduardo León
boost python uses python c apis. is it possible to start the interpreter twice by calling Py_Initialize()?
Amol Gawai
+4  A: 

Callin Py_Initialize() twice won't work well, however Py_NewInterpreter can work, depending on what you're trying to do. Read the docs carefully, you have to hold the GIL when calling this.

NicDumZ
I guess I will not get a straightforward answer to my question. Your answer has given me some inputs on which I can start work. Py_NewInterpreter seems to be the correct option to start exploring the scenario I have described. Based on this I am accepting your answer.
Amol Gawai
+1  A: 

I don't think you are the first person to want to do this, unfortunately I believe it is not possible. Are you able to run the python interperters as separate processes and use RPC?

gnibbler
A: 
  • You can let the python interpreter live outside of your application memory space. Just embed the interpreter in a DLL.
  • You can set up & save python contexts to simulate two different interpreters.
Aurélien Vallée