[Cython] Calling gil-requiring function not allowed without gil

Stefan Behnel stefan_ml at behnel.de
Thu Aug 18 08:52:44 CEST 2011

Robert Bradshaw, 17.08.2011 20:19:
> I have to admit that all these special gil
> declarations are a bit messy. I'd also rather introduce clear
> decorators, e.g.
> @cython.requires_gil  # expects gil
> cdef a(): ...
> @cython.requires.gil(False) # nogil
> cdef b(): ...
> @cython.aquires_gil  # with gil
> cdef c(): ...

The last would spell "@cython.aquire_gil", and yes, it totally makes sense 
to turn these into decorators. There's no reason at all why pure mode needs 
to differ from Cython mode here (however tricky it may be to write a GIL 
free function in pure mode...).

> (Actually, now that we have the "with gil" statement, it could be
> worth considering simply noticing the pattern of the entire function
> body in a with gil block/as the first statement and acquiring the GIL
> before argument parsing.)


> Note that we need to declare functions as requiring the GIL to allow
> for declaring cpython.pxd if extern functions are implicitly nogil.

Yes, I thought of that, too. In the specific case of .pxd files for 
external C libraries, a special "requires gil" syntax makes sense, as you'd 
want to put it at the "cdef extern" declaration once, instead of having to 
add a decorator to each of the declared functions.


More information about the cython-devel mailing list