This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author terry.reedy
Recipients serhiy.storchaka, terry.reedy
Date 2018-05-13.04:02:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1526184152.45.0.682650639539.issue33479@psf.upfronthosting.co.za>
In-reply-to
Content
(Proposed patch below)

Library Reference Chapter 25, Graphical User Interfaces with Tk, covers tinter, some of its subpackages, and IDLE.  The introduction, https://docs.python.org/3/library/tk.html states "the internal module _tkinter provides a threadsafe mechanism which allows Python and Tcl to interact."  Or as Martin Loewis claimed, "Tkinter is thread-safe." (#11077, msg127979).

Unfortunately, the unqualified 'threadsafe' claim is not true.  If Tcl is compiled with thread support (the default for 8.6) worker thread calls work (for the examples I have tested).  If not (the default for 8.5 and before), they are unreliable and can fail in multiple ways.  Known factors include the number of worker threads making call, the type of calls, and the number of calls.

The deceptive claim and the lack of knowledge about the crucial role of how tcl is compiled has negative effects.  First, people who have non-thread tcl have suffered trying to deal with random failures.  (See issues listed in #11077, msg183774, and #33257.)   Second, non-coredev tkinter experts have spread the equally wrong claim that 'tkinter is not threadsafe".  (See the same message.)  So people who have do have thread tcl are discouraged from exploiting the fact.

I suggest

1. Remove 'threadsafe' from the intro sentence.  After it, add "See <Tkinter and threads> for more details." <..> indicates a link to the proposed new section below.

2. The tkinter section itself, https://docs.python.org/3/library/tkinter.html, currently says nothing about threads.  I propose to add a new subsection, location to be decided.
"""
Tkinter and threads.

If you want to use both tkinter and threads, the safest method is to make all tkinter calls in the main thread.  If worker threads generate data needed for tkinter calls, use a queue.Queue to send the data to the main thread.  For a clean shutdown, add a method to wait for threads to stop and have it called when the window close button [X] is pressed.

If you are using Tcl/Tk compiled with thread support, you can skip the queue and call tkinter methods in the worker threads.  To avoid deadlocks when shutting down, you may have to join working threads in a separate 'join' thread that does not make tkinter calls.

If you are using Tcl/Tk compiled without thread support, tkinter calls in worker threads may initially work, but may eventually fail somewhat randomly.  Known factors include the number of threads, the type of call, and the number of calls.

To determine whether your tcl/tk has thread support, look in <install-dir>/Lib/DLLs for files such as, tcl86(t).dll and tk86(t).dll (The tcl/tk version would then be '8.6'.)  The t for 'thread' suffix is present or absent as tcl/tk was compiled with or without thread support.
<Add info for Linux and MacOS.>

<Consider adding code examples.>
"""
History
Date User Action Args
2018-05-13 04:02:32terry.reedysetrecipients: + terry.reedy, serhiy.storchaka
2018-05-13 04:02:32terry.reedysetmessageid: <1526184152.45.0.682650639539.issue33479@psf.upfronthosting.co.za>
2018-05-13 04:02:32terry.reedylinkissue33479 messages
2018-05-13 04:02:31terry.reedycreate