[Pyrex] Callbacks from threads and PyGILState_Ensure/PyGILState_Release

Stefan Behnel stefan_ml at behnel.de
Wed Aug 15 16:39:38 CEST 2007


... interesting stuff should go into Cython as well ...

Thanks for the patch, lxml needs this functionality in a couple of places, so
it would be nice to have Cython support for this.

It's not a completely generic solution, though, as there are still a couple of
places where you'd want to check a few things /before/ acquiring the GIL.
Those would still require an additional wrapper function.

Maybe we could have a more funny construct like

  with __GIL:
      ....

that would let Pyrex/Cython consider the GIL not to be set around that block?
But that feels like too much magic to me already...

Stefan


Ulisses Furquim wrote:
> On 8/14/07, Ulisses Furquim <ulisses.silva at openbossa.org> wrote:
>> 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)?
> 
> I couldn't find a _good_ way of doing this. I had the option to create
> another wrapper for my callbacks just to surround the callback with
> calls to PyGILState_Ensure and PyGILState_Release but that is really
> bad as I have lots of callbacks. Then I thought it would be good to
> have support in Pyrex for this and I wrote the attached patch for
> Pyrex-0.9.5.1a.
> 
> The patch adds a reserved word '__grabgil' that can be added to cdef
> functions to automatically generate calls to PyGILState_Ensure and
> PyGILState_Release at the right places. One can use it like this:
> 
> cdef int my_callback(void *arg) __gragil:
>     # the callback code calling a python function goes here
>     pass
> 
> In this first version of the patch '__grabgil' must be the last token
> before ':' but that can be easily changed. So.. what do you think? Is
> that something you'd like to have in Pyrex? (I think it'd be good) If
> you think so I'd be very happy to listen your suggestions/comments to
> make it a mergeable patch.
> 
> Best regards,
> 
> -- Ulisses

-------------- next part --------------
A non-text attachment was scrubbed...
Name: pyrex-grabgil.patch
Type: text/x-diff
Size: 4872 bytes
Desc: not available
Url : http://lists.copyleft.no/pipermail/pyrex/attachments/20070815/1578662a/attachment.bin 


More information about the Pyrex mailing list