
Bob Rossi, 27.06.2012 18:24:
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();
Are you embedding Python in another program? Otherwise, you don't need to do that. Python will do it on normal startup.
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
Yes. It's locking on the GIL. That's a single lock that protects the whole Python runtime.
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?
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