mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-07-29 09:29:46 +03:00
Update methods of ThreadedConnectionPool with elegant contextmanager syntax
This commit is contained in:
parent
1b255b7dc3
commit
d9a1d86ec6
17
lib/pool.py
17
lib/pool.py
|
@ -160,28 +160,21 @@ class ThreadedConnectionPool(AbstractConnectionPool):
|
||||||
import threading
|
import threading
|
||||||
AbstractConnectionPool.__init__(
|
AbstractConnectionPool.__init__(
|
||||||
self, minconn, maxconn, *args, **kwargs)
|
self, minconn, maxconn, *args, **kwargs)
|
||||||
|
|
||||||
self._lock = threading.Lock()
|
self._lock = threading.Lock()
|
||||||
|
|
||||||
def getconn(self, key=None):
|
def getconn(self, key=None):
|
||||||
"""Get a free connection and assign it to 'key' if not None."""
|
"""Get a free connection and assign it to 'key' if not None."""
|
||||||
self._lock.acquire()
|
with self._lock:
|
||||||
try:
|
|
||||||
return self._getconn(key)
|
return self._getconn(key)
|
||||||
finally:
|
|
||||||
self._lock.release()
|
|
||||||
|
|
||||||
def putconn(self, conn=None, key=None, close=False):
|
def putconn(self, conn=None, key=None, close=False):
|
||||||
"""Put away an unused connection."""
|
"""Put away an unused connection."""
|
||||||
self._lock.acquire()
|
with self._lock:
|
||||||
try:
|
|
||||||
self._putconn(conn, key, close)
|
self._putconn(conn, key, close)
|
||||||
finally:
|
|
||||||
self._lock.release()
|
|
||||||
|
|
||||||
def closeall(self):
|
def closeall(self):
|
||||||
"""Close all connections (even the one currently in use.)"""
|
"""Close all connections (even the one currently in use.)"""
|
||||||
self._lock.acquire()
|
with self._lock:
|
||||||
try:
|
|
||||||
self._closeall()
|
self._closeall()
|
||||||
finally:
|
|
||||||
self._lock.release()
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user