Skip to content

Commit aae4c25

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.15] gh-140488: Do not poll in the tkinter event loop at the interactive prompt (GH-162557) (GH-162633)
On Unix with threaded Tcl, wait for events or input instead of polling. (cherry picked from commit 0e802fd) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7fa75e3 commit aae4c25

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
@@ -3597,18 +3597,31 @@ EventHook(void)
35973597
else if (type == FILE_TYPE_DISK) {
35983598
break;
35993599
}
3600+
#else
3601+
if (tcl_lock == NULL) {
3602+
/* Tcl is threaded and stdin is watched by MyFileProc, so we
3603+
can wait for an event or input instead of polling. */
3604+
Py_BEGIN_ALLOW_THREADS
3605+
tcl_tstate = tstate;
3606+
result = Tcl_DoOneEvent(0);
3607+
tcl_tstate = NULL;
3608+
Py_END_ALLOW_THREADS
3609+
}
3610+
else
36003611
#endif
3601-
Py_BEGIN_ALLOW_THREADS
3602-
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
3603-
tcl_tstate = tstate;
3612+
{
3613+
Py_BEGIN_ALLOW_THREADS
3614+
if(tcl_lock)PyThread_acquire_lock(tcl_lock, 1);
3615+
tcl_tstate = tstate;
36043616

3605-
result = Tcl_DoOneEvent(TCL_DONT_WAIT);
3617+
result = Tcl_DoOneEvent(TCL_DONT_WAIT);
36063618

3607-
tcl_tstate = NULL;
3608-
if(tcl_lock)PyThread_release_lock(tcl_lock);
3609-
if (result == 0)
3610-
Sleep(Tkinter_busywaitinterval);
3611-
Py_END_ALLOW_THREADS
3619+
tcl_tstate = NULL;
3620+
if(tcl_lock)PyThread_release_lock(tcl_lock);
3621+
if (result == 0)
3622+
Sleep(Tkinter_busywaitinterval);
3623+
Py_END_ALLOW_THREADS
3624+
}
36123625

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

0 commit comments

Comments
 (0)