[Pythonmac-SIG] threads in OSX IDLE

Jeff Senn senn@maya.com
Wed, 03 Jul 2002 09:43:36 -0400


Jack Jansen <Jack.Jansen@oratrix.com> writes:
> Have you tried threads in IDLE on other (non-OSX) platforms? I could
> imagine that threads and IDLE (or even threads and Tkinter) don't go
...

I'm doing that now... The same version under Linux seems to work...
(but that may be lucky -- see below)

> If it works on other platforms then try getting a stacktrace: start
> Python under gdb and get a traceback after it crashed.

<sigh> it seems to have something to do with inserting the text into
the idle (Tk) window from the "other thread".  Top of the stack dump below.

So... Tk itself is not thread safe ... I've had luck trying to limit
Tkinter calls to only the main(first) thread in a multi-threaded
Python app.  I don't know how idle works, but I would guess it is (at
least) trapping stdin/out and turning them into calls to insert into
the "shell" window.  Instead these probably need to be queued back to
the main thread in a thread-safe way to avoid such crashes... (this is
probably a lot of work...)

Alternatively I think one could make a Tkinter that would queue all
calls back to a single thread.  (I have to think more about this...
the difficulty lies in blocking the thread on the next window event OR
the next queued request...)

-Jas

--------
#0  0x0115bb1c in Tk_FreeGC (display=0x0, gc=0x2eaacf0) at ../generic/tkGC.c:300
#1  0x0118d334 in FreeDLines (textPtr=0x325cf0, firstPtr=0x2e62130, lastPtr=0x0, unlink=1) at ../generic/tkTextDisp.c:732
#2  0x01190fe8 in TkTextChanged (textPtr=0x2e62130, index1Ptr=0x2e5a090, index2Ptr=0x325cf0) at ../generic/tkTextDisp.c:2737
#3  0x01187ec4 in InsertChars (textPtr=0x2e62130, indexPtr=0x2e59fe0, string=0x0) at ../generic/tkText.c:1237
#4  0x01186f68 in TextWidgetCmd (clientData=0x325cf0, interp=0x33d6c0, argc=0, argv=0x325cf0) at ../generic/tkText.c:723
#5  0x00c1d1a4 in TclInvokeStringCommand ()
#6  0x00c62144 in TclExpandTokenArray ()
#7  0x00c62474 in Tcl_EvalObjv ()
#8  0x00b23e00 in Tkapp_Call (self=0x325cf0, args=0x2e62130) at ./Modules/_tkinter.c:623#9  0x004384c8 in fast_cfunction (func=0x0, pp_stack=0x0, na=13109488) at Python/ceval.c:3157
#10 0x00435b84 in eval_frame (f=0x2e60b70) at Python/ceval.c:2020
#11 0x00437018 in PyEval_EvalCodeEx (co=0xf47820, globals=0x11cbb00, locals=0xc808f0, args=0xf661bc, argcount=4, kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:2601