1

I embedded Python in C++ using Boost Python.

I wanted to run 2 Python scripts concurrently. The scripts should also have the opportunity to access C++ member functions.

Well, when I use just 1 thread in the main interpreter it can use the member functions. But to run 2 scripts concurrently I create a new interpreter for each thread (http://docs.python.org/2/c-api/init.html#sub-interpreter-support). So scripts can be executed concurrently but they are not able to use the member functions.

This is just the general problem.


PyThreadState* Py_NewInterpreter():

Create a new sub-interpreter. This is an (almost) totally separate environment for the execution of Python code. In particular, the new interpreter has separate, independent versions of all imported modules, including the fundamental modules builtins, main and sys. The table of loaded modules (sys.modules) and the module search path (sys.path) are also separate. The new environment has no sys.argv variable. It has new standard I/O stream file objects sys.stdin, sys.stdout and sys.stderr (however these refer to the same underlying file descriptors).

So, is there no possibility to do that?

  • What C++ member functions are you talking about? – Vitaly Isaev Mar 18 '14 at 20:05
  • Functions within the C++ Class which are exposed for Python, so it can be used in the Scripts. For example you can see a Hello World class exposed and used right on this link: http://www.boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/python/exposing.html – user3433065 Mar 19 '14 at 08:08
  • "The global interpreter lock is also shared by all threads, regardless of to which interpreter they belong." If you want to run two scripts concurrently, you need to fork new processes. – Nathan Binkert Mar 21 '14 at 04:42
  • @NathanBinkert I have no problems with the GIL. And the function fork is available on a Unix System. I'm working on a Windows System and I found CreateProcess (http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx) for Windows, but the first parameter must be an application. Where I have to call a function. – user3433065 Mar 21 '14 at 10:57

0 Answers0