
Hi,
I'm really having trouble understanding the python threading model.
It's clear to me from the docs, http://docs.python.org/py3k/c-api/init.html?highlight=initthread that I need to initialize python as so when requiring the use of threads, Py_Initialize(); PyEval_InitThreads();
Then, after that point, in each new thread, I should use PyGILState_STATE state = PyGILState_Ensure(); // python code PyGILState_Release(state);
A few questions arise which do not seem to be documented clearly.
It's not clear to me if a call to Ensure locks python into executing only in that thread until Release is called, or if it's just a registration function which then allows python to choose which python code it would like to execute in which thread at which time. Can anyone answer this?
Based on my confusion above, I'm wondering if I can wrap the Ensure/Release calls around the entire new thread, or should this be done around a small snippet of python code? What are the trade offs?
Finally, I notice that after my call to PyEval_InitThreads() I had to call PyEval_SaveThread(); in order to allow other threads to call PyGILState_Ensure() with out blocking forever. This is not documented. I'm not sure I'm doing the correct thing here. Any ideas?
Thanks, Bob

Bob Rossi, 27.06.2012 18:24:
Are you embedding Python in another program? Otherwise, you don't need to do that. Python will do it on normal startup.
Yes. It's locking on the GIL. That's a single lock that protects the whole Python runtime.
The latter. It's more common to free it around code that you want to run in parallel (most commonly blocking I/O code) than to acquire it around code that you want to run serialised. But that's mostly just a matter of putting it. In any case, unless you use multiple interpreters (and doing so is not trivial), only one thread can run Python code at a time.
Stefan

Bob Rossi, 27.06.2012 18:24:
Are you embedding Python in another program? Otherwise, you don't need to do that. Python will do it on normal startup.
Yes. It's locking on the GIL. That's a single lock that protects the whole Python runtime.
The latter. It's more common to free it around code that you want to run in parallel (most commonly blocking I/O code) than to acquire it around code that you want to run serialised. But that's mostly just a matter of putting it. In any case, unless you use multiple interpreters (and doing so is not trivial), only one thread can run Python code at a time.
Stefan
participants (2)
-
Bob Rossi
-
Stefan Behnel