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?

4
  • What C++ member functions are you talking about? Commented Mar 18, 2014 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: boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/… Commented Mar 19, 2014 at 8: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. Commented Mar 21, 2014 at 4: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 (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. Commented Mar 21, 2014 at 10:57

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.