How to initialize state for C created threads

Hello,
I am starting threads in a C extension. If I understand correctly, successive sequences of PyGILState_Ensure() and PyGILState_Release will create and destroy the Python thread state over and over again, so e.g. objects stored with threading.Local() will not persist.
Can someone explain to me how I can initialize and destroy the thread state manually (i.e., right after creating and destroying the thread with pthreads), so that PyGILState_* will just acquire and release the GIL?
I guess I have to call PyThreadState_New(), but I am not sure what to do with the returned pointer and where to get the PyInterpreter argument from...
Thanks,
-Nikolaus
-- »Time flies like an arrow, fruit flies like a Banana.«
PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C

On 18/10/2010 1:00 PM, Nikolaus Rath wrote:
Hello,
I am starting threads in a C extension. If I understand correctly, successive sequences of PyGILState_Ensure() and PyGILState_Release will create and destroy the Python thread state over and over again, so e.g. objects stored with threading.Local() will not persist.
Can someone explain to me how I can initialize and destroy the thread state manually (i.e., right after creating and destroying the thread with pthreads), so that PyGILState_* will just acquire and release the GIL?
You can just Ensure as your thread starts and the Release as it terminates. You can then use the normal Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS macros to release the GIL as appropriate.
HTH,
Mark
participants (2)
-
Mark Hammond
-
Nikolaus Rath