Problem with callbacks from C to Python

Gordon McMillan gmcm at hypernet.com
Sun Jan 6 09:21:33 EST 2002


Tyler W. Wilson wrote:

> Okay, to partly answer my own question, but look for more answers, this
> is what I have determined: it has something to do with a threading
> issue, I think.
> 
> I tried the CallObject from with the set_callback method, and
> everything worked fine. So, given that I am trying to callback into the
> Python interpreter in response to a Windows message, it seems likely
> that Pythin is being re-entered, and does not like it much.
> 
> I looked over the threading and locking calls available, and tried a
> few things. I got to the point where it does crash, but the member
> never really gets called.
> 
> So, I need a way to call back into the Python interpreter in the proper
> state. Any ideas? I am going to get the tkinter source, and see what
> they do (I am working off the binary distribution, since the source
> ball is too large).

When your extension is loaded (or anytime it is called directly 
by Python), you need to grab the PyThreadState. One way to do it:

 PyThreadState *thisthread = PyThreadState_Swap(NULL);
 PyThreadState_Swap(thisthread); /* swap it back */

Now when C needs to callback into Python, you can use:

 PyEval_RestoreThread(thisthread);

And when you're done (returning to C):

 PyEval_SaveThread();

This makes the assumptions that the real OS thread is the same 
and that the PyThreadState pointer is still valid. I believe 
these are *normally* safe assumptions, but obviously not
*perfectly* safe assumptions.

-- Gordon
http://www.mcmillan-inc.com/



More information about the Python-list mailing list