Threading unfairness

Peter Hansen peter at engcorp.com
Sun May 19 08:50:41 EDT 2002


Tim Peters wrote:
> 
> [Matt Kimball]
> > I "fixed" the problem with inserting frequent 'time.sleep(0)' calls
> > in my compute-intensive thread, and that seemed to give my main GUI
> > thread more time to execute when a user-interface event occurred.
> 
> That works on Windows because the underlying Win32 API Sleep() call takes an
> argument of 0 as *meaning* "yield the timeslice".  On other OSes it may or
> may not work.  For example, people playing this trick on Solaris usually use
> time.sleep(0.01) instead, because time.sleep(0) is a nop there.

Minor, almost off-topic note:

We had been unthinkingly using time.sleep(0.01) for a while, just
kind of blindly using it as a free "yield timeslice".  We had 
some latency issues and eventually realized that on our system
10 ms is a rather long time to sleep sometimes.  We changed to
time.sleep(0.001), again without decent analysis that would let me
back up any specific claims as to the effect, but our problems appeared
to go away.

Just in case people start thinking of 0.01 as an idiom for "yield
timeslice", without understanding that it can really slow down 
certain programs...

(I'll offer here to listen intently to anyone willing to share 
his or her knowledge of implementation details (under Linux) relating 
to what sleep() does with that delay... what _is_ the resolution?)

-Peter



More information about the Python-list mailing list