diff options
author | Raymond Hettinger <python@rcn.com> | 2008-01-14 21:39:24 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-01-14 21:39:24 (GMT) |
commit | da3caedc559d6314502ddc4b10cad8534fb9d8d1 (patch) | |
tree | e57f7ae511ed0ff3a1539a5583799058dfa33450 /Lib/Queue.py | |
parent | d32ed6f5117a68037e9cb7bcfd8d0f8d1611f442 (diff) | |
download | cpython-da3caedc559d6314502ddc4b10cad8534fb9d8d1.zip cpython-da3caedc559d6314502ddc4b10cad8534fb9d8d1.tar.gz cpython-da3caedc559d6314502ddc4b10cad8534fb9d8d1.tar.bz2 |
Remove Queue.empty() and Queue.full() in favor of using qsize() or trapping the Empty and Full exceptions.
Diffstat (limited to 'Lib/Queue.py')
-rw-r--r-- | Lib/Queue.py | 48 |
1 files changed, 14 insertions, 34 deletions
diff --git a/Lib/Queue.py b/Lib/Queue.py index 79b0abf..726cf3e 100644 --- a/Lib/Queue.py +++ b/Lib/Queue.py @@ -23,6 +23,7 @@ class Queue: import threading except ImportError: import dummy_threading as threading + self.maxsize = maxsize self._init(maxsize) # mutex must be held whenever the queue is mutating. All methods # that acquire mutex must release it before returning. mutex @@ -88,20 +89,6 @@ class Queue: self.mutex.release() return n - def empty(self): - """Return True if the queue is empty, False otherwise (not reliable!).""" - self.mutex.acquire() - n = self._empty() - self.mutex.release() - return n - - def full(self): - """Return True if the queue is full, False otherwise (not reliable!).""" - self.mutex.acquire() - n = self._full() - self.mutex.release() - return n - def put(self, item, block=True, timeout=None): """Put an item into the queue. @@ -116,20 +103,22 @@ class Queue: self.not_full.acquire() try: if not block: - if self._full(): + if self.maxsize > 0 and self._qsize() == self.maxsize: raise Full elif timeout is None: - while self._full(): - self.not_full.wait() + if self.maxsize > 0: + while self._qsize() == self.maxsize: + self.not_full.wait() else: if timeout < 0: raise ValueError("'timeout' must be a positive number") endtime = _time() + timeout - while self._full(): - remaining = endtime - _time() - if remaining <= 0.0: - raise Full - self.not_full.wait(remaining) + if self.maxsize > 0: + while self._qsize() == self.maxsize: + remaining = endtime - _time() + if remaining <= 0.0: + raise Full + self.not_full.wait(remaining) self._put(item) self.unfinished_tasks += 1 self.not_empty.notify() @@ -158,16 +147,16 @@ class Queue: self.not_empty.acquire() try: if not block: - if self._empty(): + if not self._qsize(): raise Empty elif timeout is None: - while self._empty(): + while not self._qsize(): self.not_empty.wait() else: if timeout < 0: raise ValueError("'timeout' must be a positive number") endtime = _time() + timeout - while self._empty(): + while not self._qsize(): remaining = endtime - _time() if remaining <= 0.0: raise Empty @@ -192,20 +181,11 @@ class Queue: # Initialize the queue representation def _init(self, maxsize): - self.maxsize = maxsize self.queue = deque() def _qsize(self): return len(self.queue) - # Check whether the queue is empty - def _empty(self): - return not self.queue - - # Check whether the queue is full - def _full(self): - return self.maxsize > 0 and len(self.queue) == self.maxsize - # Put a new item in the queue def _put(self, item): self.queue.append(item) |