multithreading and Tkinter

Martin v. Loewis martin at v.loewis.de
Mon Apr 1 03:38:55 EST 2002


James Logajan <JamesL at Lugoj.com> writes:

> Has anyone ever tried using after_idle in a worker thread as a way to 
> signal a Tkinter mainloop thread? I'm wondering if this proposal is 
> really thread safe, since technically a worker thread is making a call to 
> Tk (even if it is nothing more than registering a callback) while the 
> mainloop thread may also be in the process of making calls to Tk.

That cannot happen. _tkinter maintains a Tcl lock, guaranteeing that
only one Python thread at any time calls into Tcl. 

You may wonder how you then can call in to Tcl while another Python
thread calls tk_mainloop: tk_mainloop does not call a Tcl loop, but
calls Tcl_DoOneEvent; if there was no event, it sleeps for 20ms
(giving up the Tcl lock during that time). As a result, other threads
can call into Tcl while Tcl is idle. If Tcl is active, those other
threads will block in the Tcl lock.

People have been complaining about the presence of the Tcl lock, and
the Sleep call (which means that there might be a 20ms delay between
an event and the time when processing the even starts). You are
encouraged to study the source of _tkinter.c, both to get trust in my
statements, and to suggest improvements to the status quo.

HTH,
Martin



More information about the Python-list mailing list