Problem with callbacks from C to Python

Tyler W. Wilson tyler.w.wilson at gte.net
Sun Jan 6 10:30:57 EST 2002


"Gordon McMillan" <gmcm at hypernet.com> wrote in message
news:Xns918E5F467E925gmcmhypernetcom at 199.171.54.213...
> 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/

Almost. I tried something like before (using the simpler
PyThreadState_Get(). But when I make the call into the Python interpreter,
it appears I hit a deadlock condition. Perhaps what is complicating matters
is that I am using Tk as well. Looking through the _tkinter.c file, there is
a TCL lock that they use too.

So, I think there is one more 'trick' to getting this thing working. Any
ideas?

Thanks,
Tyler





More information about the Python-list mailing list