Skip to content

Commit 0e802fd

Browse files
gh-140488: Do not poll in the tkinter event loop at the interactive prompt (GH-162557)
On Unix with threaded Tcl, wait for events or input instead of polling. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7824032 commit 0e802fd

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The :mod:`tkinter` event loop running at the interactive prompt no longer
2+
polls on Unix if Tcl is threaded. It now waits for events or input, so the
3+
process does not wake up every few milliseconds while idle.

‎Modules/_tkinter.c‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3792,18 +3792,31 @@ EventHook(void)
37923792
else if (type == FILE_TYPE_DISK) {
37933793
break;
37943794
}
3795+
#else
3796+
if (tcl_lock == NULL) {
3797+
/* Tcl is threaded and stdin is watched by MyFileProc, so we
3798+
can wait for an event or input instead of polling. */
3799+
Py_BEGIN_ALLOW_THREADS
3800+
tcl_tstate = tstate;
3801+
result = Tcl_DoOneEvent(0);
3802+
tcl_tstate = NULL;
3803+
Py_END_ALLOW_THREADS
3804+
}
3805+
else
37953806
#endif
3796-
Py_BEGIN_ALLOW_THREADS
3797-
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
3798-
tcl_tstate = tstate;
3807+
{
3808+
Py_BEGIN_ALLOW_THREADS
3809+
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
3810+
tcl_tstate = tstate;
37993811

3800-
result = Tcl_DoOneEvent(TCL_DONT_WAIT);
3812+
result = Tcl_DoOneEvent(TCL_DONT_WAIT);
38013813

3802-
tcl_tstate = NULL;
3803-
if(tcl_lock)PyThread_release_lock(tcl_lock);
3804-
if (result == 0)
3805-
Sleep(Tkinter_busywaitinterval);
3806-
Py_END_ALLOW_THREADS
3814+
tcl_tstate = NULL;
3815+
if(tcl_lock)PyThread_release_lock(tcl_lock);
3816+
if (result == 0)
3817+
Sleep(Tkinter_busywaitinterval);
3818+
Py_END_ALLOW_THREADS
3819+
}
38073820

38083821
/* Report an exception raised in a callback, but keep pumping events
38093822
instead of returning to the prompt: without readline there is no

0 commit comments

Comments
 (0)