The title has it, but here are some elaborations. Suppose the main thread spawns another thread, where some code is loaded into python interpreter and then another thread is called which executes some more code through the same python interface (through PyImport or PyRun). Is such scenario feasable?
Asked
Active
Viewed 1,328 times
5
-
1You want two threads sharing the same embedded interpreter right? – mshildt Jul 26 '16 at 13:27
-
@epicbrew yep, the same interpreter, both threads having access to the same memory – user3496846 Jul 26 '16 at 15:29
1 Answers
2
If I'm following what you are asking, then yes you can do this, but the Python interpreter itself is not fully thread safe. To get around this, you must make sure each thread obtains the interpreter's GIL before calling any Python code and then releases it afterwards. i.e. Each thread needs to do the following when executing Python code:
PyGILState_STATE gstate;
gstate = PyGILState_Ensure();
// Do any needed Python API operations, execute python code
// Release the GIL. No Python API allowed beyond this point.
PyGILState_Release(gstate);
Also you should do the following after starting the Python interpreter to ensure threads/GIL are properly initialized:
if (! PyEval_ThreadsInitialized()) {
PyEval_InitThreads();
}
See Non Python Created Threads for more info on this.
As mentioned in the comments, it's worth noting that this is really just serializing access to the interpreter, but it's the best you can do assuming you are using the CPython implementation of Python.

mshildt
- 8,782
- 3
- 34
- 41
-
Are you sure, Epic? It sure looks to me like you're (at best ...) *serializing* the thread's use of a single interpreter instance. It seems to me that the OP wants each (C++) thread to have its own Python interpreter-instance and to be able to run both in parallel, with the threading being done by C++, not Python. Or, am I mis-reading the OP's intent? – Mike Robinson Jul 26 '16 at 13:21
-
@MikeRobinson I thought he wanted two threads sharing the same interpreter. – mshildt Jul 26 '16 at 13:25
-
Yeah, and, even though Python's "threads" are "just 'sort-of' threads" (because, of course, Python is an interpreter ...), I kinda sorta think that I would put my threading into Python code. We know that Python correctly manages *its* version of threading ... – Mike Robinson Jul 26 '16 at 13:47
-
Actually Python's threads are real native threads. But they have to acquire the GIL before executing Python bytecode. – mshildt Jul 26 '16 at 13:51
-
Yep ... and it might be un-feasible for the OP to re-tool his application. – Mike Robinson Jul 26 '16 at 14:03
-
@MikeRobinson you got me right, I want to run both threads in parallel. – user3496846 Jul 26 '16 at 15:24
-
@epicbrew oh, well, that's a good answer, albeit not very promising. But since its the closest thing to what I asked, I will mark it as answer. Thank you! – user3496846 Jul 26 '16 at 15:24