From 766f06f290c8acc6a28116806fde3e43708538eb Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Tue, 21 Sep 2021 22:46:30 +0530 Subject: [PATCH] Update methods of ThreadedConnectionPool Updated methods of the ThreadedConnectionPool class to use the elegant context manager syntax. --- lib/pool.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/lib/pool.py b/lib/pool.py index f29fbf53..0fbca902 100644 --- a/lib/pool.py +++ b/lib/pool.py @@ -173,7 +173,7 @@ class ThreadedConnectionPool(AbstractConnectionPool): Usually these are passed to the underlying ``connect`` method of psycopg2 """ - + import threading AbstractConnectionPool.__init__( self, minconn, maxconn, *args, **kwargs) @@ -181,24 +181,15 @@ class ThreadedConnectionPool(AbstractConnectionPool): def getconn(self, key=None): """Get a free connection and assign it to 'key' if not None.""" - self._lock.acquire() - try: + with self._lock: return self._getconn(key) - finally: - self._lock.release() def putconn(self, conn=None, key=None, close=False): """Put away an unused connection.""" - self._lock.acquire() - try: + with self._lock: self._putconn(conn, key, close) - finally: - self._lock.release() def closeall(self): """Close all connections (even the one currently in use.)""" - self._lock.acquire() - try: + with self._lock: self._closeall() - finally: - self._lock.release()