Problem with callbacks from C to Python

Tyler W. Wilson tyler.w.wilson at gte.net
Sun Jan 6 13:55:11 EST 2002


"Tyler W. Wilson" <tyler.w.wilson at gte.net> wrote in message
news:R4_Z7.867$Ml3.253356 at dfiatx1-snr1.gtei.net...
> "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
>
Even more information: it looks like it is a Tk reentrancy issue. If I do
not make any tkinter calls within my Python callback, everything is fine.
But, if I try anything with Tk, it deadlocks.

I have looked through the code for a number of modules, but I do not see
anything that looks like my case, which I find hard to believe. Checking
win32all now...

BTW, I also tried doing some sort of hack with custom bindings, to no avail.
What I could use is some way to post an event to the TCL/Tk event queue...

Thanks,
Tyler





More information about the Python-list mailing list