[Python-bugs-list] [ python-Feature Requests-780602 ] No sleep or busy wait

SourceForge.net noreply at sourceforge.net
Sun Sep 7 16:48:01 EDT 2003


Feature Requests item #780602, was opened at 2003-07-30 19:14
Message generated for change (Comment added) made by jbrouwers
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=780602&group_id=5470

Category: Tkinter
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Jean M. Brouwers (jbrouwers)
Assigned to: Nobody/Anonymous (nobody)
Summary: No sleep or busy wait

Initial Comment:
This is a request for three, related changes to file

Modules/_tkinter.c in Python 2.3, specifically to avoid

sleeping or busy waiting.





1) The main loop, function Tkapp_Mainloop(), includes a

now configurable sleep period which may cause

undesirable delays (in the threaded builds).  If no

Tcl/Tk event was handled, the main loop sleeps for 20

millisec (or whatever the value of

Tkinter_busywaitinterval* is).



Another, better way to achieve that would be to create

a timer event for in the main loop BEFORE calling the

Tcl/Tk event handler and call the Tcl/Tk event handler

with a zero argument to wait for an event**.



This produces the same idle period as the current

implementation.  The difference is that the main loop

(actually the Tcl/Tk event handler) stays awake instead

of going to sleep for the idle period.  Any events

occurring during the idle period will be handled

without delay.





2) In addition, only one event is handled per iteration

of the main loop, causing the state to be saved and

restored, locks to be acquired and releases, etc. many

times.



Instead handle not one event, but all pending events. 

After handling one event, all other pending events

should be handled as well by calling the Tcl/Tk event

handler again (with a TCL_DONT_WAIT argument) until

there are no more events.



The net result is that any pending events are handled

more quickly and with less overhead.





3) After the first change abiove, static function

Sleep() can be removed.  It is still needed in function

WaitForMainloop() but that call can be replaced by

Tcl_Sleep().  Tcl_Sleep() is equivalent to Sleep() and

it works on all platforms, not just those which have

select().



--------

*) The name Tkinter_busywaitinterval is a misnomer.  It

is not a busy wait but a non-buzy sleep, at least with

Tcl_Sleep().



**) There are a few more details to make this work

correctly and I have a version for _tkinter.c which

does implement these changes.

----------------------------------------------------------------------

>Comment By: Jean M. Brouwers (jbrouwers)
Date: 2003-09-07 17:48

Message:
Logged In: YES 
user_id=832557

No, I do not know of any platforms where the current Sleep()

implementation in Tkinter would not work.  That aside, I

believe that there are still two issues with using Sleep().



>From previous experience with Tcl/Tk (non-Python), I do know

that events tend to pile up in single-threaded, event driven

applications which use Tcl timer events.  In such

applications calls like Sleep() or Tcl_Sleep() should be

avoided entirely.  Using Tcl/Tk timer events is a far better

approach in this case, for reasons mentioned earlier.



I'm not familiar enough with Tkinter, multi-threading and

the impact of Sleep() in Python.  However, instead of using

Sleep(), what about yield() or some similar threading

function?  And compile that call only in the threaded Python

build?



----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-09-07 14:37

Message:
Logged In: YES 
user_id=21627

1) The main-loop is inherently *busy*-wait, as the loop is

continuously polling for events. The interval between two of

these wait-for-event calls is then indeed a busywaitinterval.



2) Processing all events would mean that other threads

cannot invoke Tcl methods while there are pending requests.

This would be a change in behaviour, and might influence

responsiveness in a undesirable way.



3) Can you name a platform on which the current Sleep does

not work?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=780602&group_id=5470



More information about the Python-bugs-list mailing list