I'd like to propose a signature simplification and a new method for the Queue.Queue class:
1) Drop the optional `block` argument from put() and get(), since all the meaningful combinations of (block, timeout) are equivalent to passing block = (timeout is None or timeout>0). IOW, instead of passing block=False pass timeout=0 (or negative). Obviously this is to be considered for 3.x only.
2) Add a new `rotate` method as the atomic equivalent of put_nowait(get_nowait()). Currently I use the following subclass but it would be nice to have it in the base Queue:
def rotate(self, n=1): '''Rotate this queue n steps to the left (if it is not empty).
Rotating one step is equivalent to an atomic q.put_nowait(q.get_nowait()). ''' if n < 0: raise ValueError('n must be non-negative') self.mutex.acquire() try: if not self._empty(): self._rotate(n) finally: self.mutex.release()
def __init__(self, maxsize=0): Queue.__init__(self, maxsize) if hasattr(self.queue, 'rotate'): # deque has rotate() since v2.5 def _rotate(n): # negative n for left rotate self.queue.rotate(-n) else: def _rotate(n): put,get = self._put, self._get for i in xrange(n): put(get()) self._rotate = _rotate
What do you think ?