asynchronous callback

Donn Cave donn at
Wed Aug 22 07:03:40 CEST 2001

Quoth Silvio Arcangeli <sarcangeli at>:
| In my case the callback function should actually be a Python method.
| So I added a C callback function to my extension module: it just converts 
| the parameters, and then calls the Python method.
| Anyway, the C function correctly retrieves the Python method from the 
| dictionary, but then when it calls it it gets blocked.
| I think that this is probably due to the fact that the C callback function 
| is called asynchronously (that means it runs in a different thread from the 
| interpreter's one).
| How can I make it work?

I don't know if I would have expected it to block, but at any rate,
any healthy callback needs to acquire the interpreter's lock before
proceeding.  Here's how mine do it -

   /* Before the thread starts, get a thread state object for it. */
   tstate = PyThreadState_New(PyThreadState_Get()->interp);

   /* From that thread, about to call from C into Python. */

   /* After Python function returns. */

Meanwhile, of course functions that call out to C for any length
of time must release the interpreter lock first, so your callback
can get it!  Usually done with macros, Py_BEGIN_ALLOW_THREADS

I hope that's right, it has been a while since I had to wrestle
with this.

	Donn Cave, donn at

More information about the Python-list mailing list