Compile thread-safe tcl for python on Windows
Question: I'm doing a project with Python and I need to put something in thread. It
turned out that if you do something that uses Tk in thread, it will somehow
crash. The error is:

    
    
    TclError: out of stack space (infinite loop?)`
    

I searched on Google and I think this perhaps because Tcl is not thread-safe.
When I ran this I got the Tcl error:

    
    
    import Tkinter    
    Tkinter.Tk().getvar("tcl_platform(threaded)")
    

It is said recompiling tcl with --enable-threads could fix this problem. My
question is how to recompiling tcl in Windows. And how to replace the current
one with the compiled one. I'm using Python 2.7 and Tcl 8.5

Thanks


Answer: **Summary:** Each Tk widget must only be used from a single thread; there's a
lot of thread-specific data in use inside the implementation, so that's a
really hard requirement. Your hacking is not going to get around this.

**Details:** Python communicates with Tcl under the covers to work with Tk,
and threaded Tcl is designed to be strongly thread-bound (so as to avoid
having things like the GIL). It's possible to use a non-threaded build, but
then you get hit by problems with the code to guard against stack overflows (a
very nasty hack under the covers) which gets confused by the existence of
multiple C stacks when it thinks it is unthreaded. **_This is the part that
particular error you report comes from._** The stack check code is removed in
Tcl 8.6 (which uses a “stackless” implementation) but that's not likely to
help as no attempts are made to deal with inter-thread locking issues unless
you're in a threaded build (and that takes you back into thread-specific data
issues).

Tk in unthreaded mode makes lots of assumptions about global shared data and
is so really unsafe to use from multiple threads (the quality of the threading
code involved is not what you might wish, with much of it dating from a time
when the underlying OS libraries were often not very good at thread handling
either). Tk in threaded mode uses TSD extensively; the only way to use Tk in
threaded mode is for each thread to have its own main window and event loop,
and to _never_ mix things up between them.

The easiest thing is to keep one thread dedicated to managing the GUI (often
the main application thread) and to move all non-GUI work into other threads.

