summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/util
diff options
context:
space:
mode:
authorHeckad <heckad@yandex.ru>2020-01-01 14:47:01 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2020-01-03 13:09:46 -0500
commita076b1f30406cbb59a55e2c01ddd17a84636778e (patch)
treebb4c5a5b88d45963040842427d73f9616465bdb0 /lib/sqlalchemy/util
parent748bf710fdeeed327933a5e7c277c7f79f6053a6 (diff)
downloadsqlalchemy-a076b1f30406cbb59a55e2c01ddd17a84636778e.tar.gz
Use context managers for threading.Lock()
(zzzeek:) For some reason I thought that threading.Lock() still did not support context managers, at least in Python 2, however this does not seem to be the case. Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com> Closes: #5069 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5069 Pull-request-sha: efeac06dda5afdbe33abcf9b27c8b5b5725c8444 Change-Id: Ic64fcd99cd587bc70b4ecc5b45d8205b5c76eff2
Diffstat (limited to 'lib/sqlalchemy/util')
-rw-r--r--lib/sqlalchemy/util/langhelpers.py10
-rw-r--r--lib/sqlalchemy/util/queue.py29
2 files changed, 11 insertions, 28 deletions
diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py
index 83f119660..8124764be 100644
--- a/lib/sqlalchemy/util/langhelpers.py
+++ b/lib/sqlalchemy/util/langhelpers.py
@@ -1179,11 +1179,8 @@ def counter():
# avoid the 2to3 "next" transformation...
def _next():
- lock.acquire()
- try:
+ with lock:
return next(counter)
- finally:
- lock.release()
return _next
@@ -1362,14 +1359,11 @@ class symbol(object):
_lock = compat.threading.Lock()
def __new__(cls, name, doc=None, canonical=None):
- cls._lock.acquire()
- try:
+ with cls._lock:
sym = cls.symbols.get(name)
if sym is None:
cls.symbols[name] = sym = _symbol(name, doc, canonical)
return sym
- finally:
- symbol._lock.release()
@classmethod
def parse_user_argument(
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.