[Pyrex] Callbacks from other threads (crash in PyGILState_Release)

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Wed Jun 21 05:54:04 UTC 2006


Hi Geoff,

Geoff Schmidt wrote:
> My understanding of the docs was that PyEval_SaveThread effectively
> suspend the thread (from Python's perspective) so that another function
> had a chance to gain access to the interpreter by calling
> PyEval_RestoreThread or PyGILState_Ensure, and that
> PyGILState_Ensure/Release was basically PyEval_Save/RestoreThread plus
> logic to create and destroy thread states if necessary. Is this
> understanding correct?

Sounds correct.


>> cdef PyThreadState *state
>> state = PyEval_SaveThread()
>> function_that_calls_callback_in_thread(callback)
>> PyEval_RestoreThread(state)
> 
> My function_that_calls_callback_in_thread() returns immediately, so I
> didn't think PyEval_Save/RestoreThread was necessary

Right, then it's of no use here.

The way we use it is having threaded C code that calls into Python from within
the function call above. That's why it makes sense for us to call
save/restoreThread. If the interpreter is to be called out-of-order, all you
should need to do is make sure InitThreads was called (as Jani suggested).

Stefan




More information about the Pyrex mailing list