diff options
Diffstat (limited to 'lib/sqlalchemy/util/queue.py')
-rw-r--r-- | lib/sqlalchemy/util/queue.py | 29 |
1 files changed, 9 insertions, 20 deletions
diff --git a/lib/sqlalchemy/util/queue.py b/lib/sqlalchemy/util/queue.py index 819d95684..78b7351d4 100644 --- a/lib/sqlalchemy/util/queue.py +++ b/lib/sqlalchemy/util/queue.py @@ -66,28 +66,22 @@ class Queue: def qsize(self): """Return the approximate size of the queue (not reliable!).""" - self.mutex.acquire() - n = self._qsize() - self.mutex.release() - return n + with self.mutex: + return self._qsize() 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 + with self.mutex: + return self._empty() 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 + with self.mutex: + return self._full() def put(self, item, block=True, timeout=None): """Put an item into the queue. @@ -102,8 +96,7 @@ class Queue: (`timeout` is ignored in that case). """ - self.not_full.acquire() - try: + with self.not_full: if not block: if self._full(): raise Full @@ -121,8 +114,6 @@ class Queue: self.not_full.wait(remaining) self._put(item) self.not_empty.notify() - finally: - self.not_full.release() def put_nowait(self, item): """Put an item into the queue without blocking. @@ -142,9 +133,9 @@ class Queue: available within that time. Otherwise (`block` is false), return an item if one is immediately available, else raise the ``Empty`` exception (`timeout` is ignored in that case). + """ - self.not_empty.acquire() - try: + with self.not_empty: if not block: if self._empty(): raise Empty @@ -163,8 +154,6 @@ class Queue: item = self._get() self.not_full.notify() return item - finally: - self.not_empty.release() def get_nowait(self): """Remove and return an item from the queue without blocking. |