multiple embedded python interpreter instances in a *single*threaded application

Jan Boelsche jan at muskelfisch.com
Fri Nov 1 11:12:16 EST 2002


I want to wrap the python interpreter into a C++ class named
PythonInterpreter. Each instance of PythonInterpreter needs to have
its own independant interpreter instance.
I've read the section about Thread States in the Python C API
documentation and I understand how to create and swap PyThreadStates
to acomplish independance between interpreter instances. However what
I want to do has nothing to do with threads, I am looking for a way to
save and restore PyInterpreterState, which I think is a member of
PyThreadStates. I assume swaping PyThreadStates would
cause unnecessary overhead because of needless acquisition of the
global lock.

What I want to do is something like this:

class PythonInterpreter {
public:
  PythonInterpreter();
  bool runSkript(std::string code);
protected:
  PyInterpreterState* _state;
};

void main() {
  PythonInterpreter p1, p2;
  p1.runSKript("import sys\n");
  p2.runSKript("import sys\n");
  p1.runSKript("sys.path = 'foo'\n");
  p2.runSKript("sys.path = 'bar'\n");
}

What would be the most efficient way to acomplish this?

Thanks in advance,
  Jan



More information about the Python-list mailing list