summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util/queue.py
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2020-01-03 21:49:49 +0000
committerGerrit Code Review <gerrit@bbpush.zzzcomputing.com>2020-01-03 21:49:49 +0000
commitc0352b03e840ddf68116182b22c5fd4eac2059cf (patch)
tree46c46a3d151197f149348680fcbbc18bdc395e81 /lib/sqlalchemy/util/queue.py
parent5881fd274015af3de37f2ff0f91ff6a7c61c1540 (diff)
parenta076b1f30406cbb59a55e2c01ddd17a84636778e (diff)
downloadsqlalchemy-c0352b03e840ddf68116182b22c5fd4eac2059cf.tar.gz
Merge "Use context managers for threading.Lock()"
Diffstat (limited to 'lib/sqlalchemy/util/queue.py')
-rw-r--r--lib/sqlalchemy/util/queue.py29
1 files changed, 9 insertions, 20 deletions
diff --git a/lib/sqlalchemy/util/queue.py b/lib/sqlalchemy/util/queue.py
index 1b2d19524..3433657d6 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.