[Pyrex] Callbacks from threads and PyGILState_Ensure/PyGILState_Release

Ulisses Furquim ulisses.silva at openbossa.org
Tue Aug 14 18:03:08 CEST 2007


Hi,

I'm having a problem with callbacks from python threads (using
threading) and PyGILState_Ensure/PyGILState_Release. I need
PyGILState_Ensure to be called before any python code inside my
function and PyGILState_Release after everything. However, pyrex
generates some decrefs after my call to PyGILState_Release which is
making the interpreter segfault.

I've found this thread in the mailing list archives:

http://lists.copyleft.no/pipermail/pyrex/2006-June/001866.html

However, this thread only tells me I'm doing what I need to. My
callback is called from a main loop running on one thread created with
the threading module. Before entering the main loop I'm "calling"
Py_BEGIN_ALLOW_THREADS and I have Py_END_ALLOW_THREADS afterwards.
Thus, inside a callback from this main loop I need to grab the GIL. My
callback looks like this:

cdef int idler_cb(void *_td):
   cdef int r

   cdef python.PyGILState_STATE state
   state = python.PyGILState_Ensure()

   obj = <Idler>_td

   try:
       r = bool(obj._exec())
   except Exception, e:
       import traceback
       traceback.print_exc()
       r = 0

   if not r:
       obj.delete()

   python.PyGILState_Release(state)

   return r

Is there any way to tell pyrex that my call  to PyGILState_Release
needs to be the last thing executed inside my function (before
returning, of course)?

Best regards,

-- Ulisses



More information about the Pyrex mailing list