reentrant callbacks from C into Python

Greg Chapman glc at well.com
Tue Mar 4 11:21:20 EST 2003


On 02 Mar 2003 20:07:17 -0500, gb at cs.unc.edu wrote:

>Problem is that some of the functions in the API can cause the
>callback to be reentered. So, I'm processing a callback and one of the
>calls in the Python code causes the C callback to be reentered.
>
>Now when I try to acquire the GIL, deadlock ensues. 
>
>I've made a hack workaround with a static variable keeping track of
>the number of times I've locally acquired the GIL. If I already have
>it I don't try to get it again. That appears to work but I wonder if
>it would work in the presence of threads.
>
>I thought about releasing the GIL around the API functions that I am
>wrapping but they warn the reentry might happen in lots of functions
>outside the ActiveAccessibility and WinEvents APIs. So it is hard to
>be sure that the GIL won't already be held when I enter the C
>callback.
>
>There *must* be a better way. Is there a standard way to deal with a
>problem like this?

Right now, as I understand it, your best bet is to surround all calls to API
functions with Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS and to hope that
all other modules in the project do the same.  (I believe the modules included
in the distribution and the win32all modules are pretty good about this).

Soon (hopefully), the answer will be the new API outlined by PEP 311:

    http://www.python.org/peps/pep-0311.html

---
Greg Chapman





More information about the Python-list mailing list