From d5f8cab9c26fb497b024cba3947d0b6506017680 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 24 Nov 2026 15:53:50 +0200 Subject: [PATCH] gh-140488: Do not poll in the tkinter event loop at the interactive prompt On Unix with threaded Tcl, wait for events or input instead of polling. Co-Authored-By: Claude Opus 5.5 --- ...-11-24-17-00-00.gh-issue-140488.Bw4tIq.rst | 3 ++ Modules/_tkinter.c | 31 +++++++++++++------ 2 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-24-17-00-00.gh-issue-140488.Bw4tIq.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-24-17-00-00.gh-issue-140488.Bw4tIq.rst b/Misc/NEWS.d/next/Library/2026-11-24-17-00-00.gh-issue-140488.Bw4tIq.rst new file mode 100644 index 00000000000000..ad13320d20a82d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-24-17-00-00.gh-issue-140488.Bw4tIq.rst @@ -0,0 +1,3 @@ +The :mod:`tkinter` event loop running at the interactive prompt no longer +polls on Unix if Tcl is threaded. It now waits for events or input, so the +process does not wake up every few milliseconds while idle. diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index d0a03953a80d92..445530e2def6cb 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -3762,18 +3762,31 @@ EventHook(void) else if (type == FILE_TYPE_DISK) { break; } +#else + if (tcl_lock == NULL) { + /* Tcl is threaded and stdin is watched by MyFileProc, so we + can wait for an event or input instead of polling. */ + Py_BEGIN_ALLOW_THREADS + tcl_tstate = event_tstate; + result = Tcl_DoOneEvent(0); + tcl_tstate = NULL; + Py_END_ALLOW_THREADS + } + else #endif - Py_BEGIN_ALLOW_THREADS - if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); - tcl_tstate = event_tstate; + { + Py_BEGIN_ALLOW_THREADS + if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1); + tcl_tstate = event_tstate; - result = Tcl_DoOneEvent(TCL_DONT_WAIT); + result = Tcl_DoOneEvent(TCL_DONT_WAIT); - tcl_tstate = NULL; - if(tcl_lock)PyThread_release_lock(tcl_lock); - if (result == 0) - Sleep(Tkinter_busywaitinterval); - Py_END_ALLOW_THREADS + tcl_tstate = NULL; + if(tcl_lock)PyThread_release_lock(tcl_lock); + if (result == 0) + Sleep(Tkinter_busywaitinterval); + Py_END_ALLOW_THREADS + } /* Report an exception raised in a callback, but keep pumping events instead of returning to the prompt: without readline there is no