Fixes for TimeoutQueue.

This commit is contained in:
Jeremy Fincher 2004-09-08 22:52:30 +00:00
parent 142349847b
commit cf52bf14a3

View File

@ -311,11 +311,20 @@ class TimeoutQueue(object):
self.queue = queue self.queue = queue
self.timeout = timeout self.timeout = timeout
def _getTimeout(self):
if callable(self.timeout):
return self.timeout()
else:
return self.timeout
def _clearOldElements(self): def _clearOldElements(self):
now = time.time() now = time.time()
while now - self.queue.peek()[0] > self.timeout: while now - self.queue.peek()[0] > self._getTimeout():
self.queue.dequeue() self.queue.dequeue()
def setTimeout(self, i):
self.timeout = i
def enqueue(self, elt, at=None): def enqueue(self, elt, at=None):
if at is None: if at is None:
at = time.time() at = time.time()
@ -330,7 +339,7 @@ class TimeoutQueue(object):
# the resulting generator and elements that should've timed out are # the resulting generator and elements that should've timed out are
# yielded? Hmm? What happens then, smarty-pants? # yielded? Hmm? What happens then, smarty-pants?
for (t, elt) in self.queue: for (t, elt) in self.queue:
if time.time() - t < self.timeout: if time.time() - t < self._getTimeout():
yield elt yield elt
def __len__(self): def __len__(self):