From e9a62bf418d6ab9b8ff578f4afe612185fd17ec5 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Tue, 21 Sep 2021 22:40:31 +0530 Subject: [PATCH 1/3] Update docs Update documentation of __init__ method of ThreadedPoolConnection. The old documentation was incorrect because it says "initialze a threading lock." --- lib/pool.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/pool.py b/lib/pool.py index 9d67d68eb..f29fbf530 100644 --- a/lib/pool.py +++ b/lib/pool.py @@ -156,7 +156,24 @@ class ThreadedConnectionPool(AbstractConnectionPool): """A connection pool that works with the threading module.""" def __init__(self, minconn, maxconn, *args, **kwargs): - """Initialize the threading lock.""" + """Initializes a new instance of ThreadedConnectionPool. + + Parameters + ---------- + minconn : int + The minimum number of connections that should be automatically + created when you initialize the connection pool. + maxconn : int + The maximum number of connections that will be supported by this + connection pool + args : optional, positional arguments + Usually these are passed to the underlying ``connect`` method + of psycopg2 + kwargs : optional, keyword only arguments + Usually these are passed to the underlying ``connect`` method + of psycopg2 + """ + import threading AbstractConnectionPool.__init__( self, minconn, maxconn, *args, **kwargs) From 766f06f290c8acc6a28116806fde3e43708538eb Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Tue, 21 Sep 2021 22:46:30 +0530 Subject: [PATCH 2/3] 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 f29fbf530..0fbca902e 100644 --- a/lib/pool.py +++ b/lib/pool.py @@ -173,7 +173,7 @@ def __init__(self, minconn, maxconn, *args, **kwargs): Usually these are passed to the underlying ``connect`` method of psycopg2 """ - + import threading AbstractConnectionPool.__init__( self, minconn, maxconn, *args, **kwargs) @@ -181,24 +181,15 @@ def __init__(self, minconn, maxconn, *args, **kwargs): 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() From 5a8989e0e7fd516f762072446cee508ad71c2f05 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Tue, 21 Sep 2021 22:48:58 +0530 Subject: [PATCH 3/3] Update __init__ of ThreadedConnectionPool --- lib/pool.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pool.py b/lib/pool.py index 0fbca902e..c9d38a18e 100644 --- a/lib/pool.py +++ b/lib/pool.py @@ -174,9 +174,9 @@ def __init__(self, minconn, maxconn, *args, **kwargs): of psycopg2 """ + super().__init__(minconn, maxconn, *args, **kwargs) + import threading - AbstractConnectionPool.__init__( - self, minconn, maxconn, *args, **kwargs) self._lock = threading.Lock() def getconn(self, key=None):