[Python-checkins] python/dist/src/Lib Queue.py,1.16,1.17

Guido van Rossum guido@python.org
Tue, 15 Oct 2002 20:31:35 -0400


> !                 # waiting max. 'timeout' seconds.
> !                 # this code snipped is from threading.py: _Event.wait():
> !                 # Balancing act:  We can't afford a pure busy loop, so we
> !                 # have to sleep; but if we sleep the whole timeout time,
> !                 # we'll be unresponsive.  The scheme here sleeps very
> !                 # little at first, longer as time goes on, but never longer
> !                 # than 20 times per second (or the timeout time remaining).
> !                 delay = 0.0005 # 500 us -> initial delay of 1 ms
> !                 endtime = _time() + timeout
> !                 while True:
> !                     if self.fsema.acquire(0):
> !                         break
> !                     remaining = endtime - _time()
> !                     if remaining <= 0:  #time is over and no slot was free
> !                         raise Full
> !                     delay = min(delay * 2, remaining, .05)
> !                     _sleep(delay)       #reduce CPU usage by using a sleep

Hm, I wonder if the threading module should make this operation
(wait for a lock with a timeout) available natively.

Then on some platforms maybe the lock implementation can support this
natively and we can get rid of the silly sleeps there.

--Guido van Rossum (home page: http://www.python.org/~guido/)