mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-07-10 08:12:26 +03:00
Merge 5a8989e0e7
into c2b6a8aaea
This commit is contained in:
commit
0e7a729696
38
lib/pool.py
38
lib/pool.py
|
@ -156,32 +156,40 @@ class ThreadedConnectionPool(AbstractConnectionPool):
|
||||||
"""A connection pool that works with the threading module."""
|
"""A connection pool that works with the threading module."""
|
||||||
|
|
||||||
def __init__(self, minconn, maxconn, *args, **kwargs):
|
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
|
||||||
|
"""
|
||||||
|
|
||||||
|
super().__init__(minconn, maxconn, *args, **kwargs)
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
AbstractConnectionPool.__init__(
|
|
||||||
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