[Pyrex] [Cython-dev] Callbacks from threads and PyGILState_Ensure/PyGILState_Release

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Sep 16 03:11:09 CEST 2007


Stefan Behnel wrote:
> Ok, that's a simple example as you are passing the callback pointer *outside*
> of the GIL context *directly* into the function.

It doesn't matter when the pointer is passed, only
whether the GIL is held when the function the pointer
points to is called. The pointer could have been
stored in a global variable the day before and it
would still work.

The only thing in my example that Pyrex doesn't know
is going on is that calling do_something_heavy() with
the GIL released implies that its callback function
will also be called with the GIL released. The
programmer has to supply this knowledge by means of
declarations.

> But how would Pyrex deal with this:

I don't see how that's any different. You're just passing
the callback inside a struct instead of directly as an
argument.

To type this correctly, the struct would be declared
with a nogil function pointer type for the callback
member.

> I like the idea, but you should not try to make it perfect. There will always
> be code where Pyrex just can't know what's going on

Don't worry, there will be ways of overriding all this,
by casting function pointer types and/or using explicit
with GIL/without GIL blocks. But I'm hoping it will
only be necessary in rare circumstances.

--
Greg



More information about the Pyrex mailing list