[Python-Dev] Event loops, PyOS_InputHook, and Tkinter

Noam Raphael noamraph at gmail.com
Mon Nov 14 21:12:04 CET 2005


On 11/14/05, Michiel Jan Laurens de Hoon <mdehoon at c2b2.columbia.edu> wrote:
> Ronald Oussoren wrote:
>
> > I wonder why nobody has suggested a seperate thread for managing the
> > GUI and
> > using the hook in Python's event loop to issue the call to update_plot.
> >
> Ha. That's probably the best solution I've heard so far, short of adding
> a Tcl-like event loop API to Python.

No. It is definitely a bad solution.

Where I work, we do a lot of plotting from the interactive
interpreter, using Tkinter. I always wondered how it worked, and
assumed that it was done using threading. So when people started using
IDLE, and those plots didn't show up, I've found the solution of
calling the Tkinter main() function from a thread. Everything seemed
to work fine, until...

It didn't. Strange freezes started to appear, only when working from
IDLE. This made me investigate a bit, and I've found that Tkinter
isn't run from a seperate thread - the dooneevent() function is called
repeatedly by PyOS_InputHook while the interpreter is idle.

The conclusions:
1. Don't use threads when you don't have to. Tkinter does callbacks to
Python code, and most code isn't designed to work reliably in
multithreaded environment.
2. The non-threading solution works *really* well - the fact is that I
hadn't noticed the difference between multi-threaded mode and
single-threaded mode, until things began to freeze in the
multi-threaded mode.

Noam


More information about the Python-Dev mailing list