[Cython] 'with gil:' statement

Stefan Behnel stefan_ml at behnel.de
Wed Mar 16 15:15:41 CET 2011

Dag Sverre Seljebotn, 16.03.2011 13:37:
> On 03/16/2011 12:54 PM, mark florisson wrote:
>> On 16 March 2011 11:58, Dag Sverre Seljebotn wrote:
>>> I think we should make nested nogil-s noops, i.e.
>>> with nogil:
>>>     with nogil: # => if True:
>>> This is because one may want to change "with nogil" to "with gil" for
>>> debugging purposes (allow printing debug information).
>> Interesting, that does sound convenient, but I'm not if mere
>> convenience should move us to simply ignore what is technically most
>> likely incorrect code (unless there is intermediate manual locking).
> I'm just trying to minimize the "language getting in your way" factor. It
> is pretty useless to write
> if x:
>     if x:
>         ...
> as well, but Python does allow it.

That's because it's not necessarily useless. It can have side-effects.

>> In any case, I wouldn't really be against that. If you simply want to
>> allow this for debugging, we could also allow print statements in
>> nogil sections, by either rewriting it using 'with gil:', or by
>> inserting a simple printf (in which case you probably want to place a
>> few restrictions).
> It's not only print statements. I.e., if I think something is wrong with an
> array, I'll stick in code like
> print np.std(x), np.mean(x), np.any(np.isnan(x))
> or something more complex that may require temporaries. Or even plot the
> vector:
> plt.plot(x)
> plt.show() # blocks until I close plot window
> Or, launch a debugger:
> if np.any(np.isnan(x)):
>     import pdb; pdb.set_trace()

All of these are better expressed using an explicit "with gil", also in 
debug code.


More information about the cython-devel mailing list