summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Di Gregorio <fog@initd.org>2005-10-19 14:38:37 +0000
committerFederico Di Gregorio <fog@initd.org>2005-10-19 14:38:37 +0000
commitf687f2853ed619e4ee1b53f350cc31158e81e9cf (patch)
treef349d003dd31c72c591a15a0ebbb30d21e818b98
parentd5f639cfd6349c1e05523bd4cf21b961d1495581 (diff)
downloadpsycopg2-f687f2853ed619e4ee1b53f350cc31158e81e9cf.tar.gz
Various pool changes.
-rw-r--r--ChangeLog6
-rw-r--r--lib/pool.py42
2 files changed, 45 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 37d8aa9..070daa2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2005-10-20 Federico Di Gregorio <fog@initd.org>
+
+ * lib/pool.py: renamed ThreadedConnectionPool to PersistentConnectionPool
+ and added a connection pool that allows multiple connections per thread
+ as ThreadedConnectionPool (courtesy of Daniele Varrazzo.)
+
2005-10-19 Federico Di Gregorio <fog@initd.org>
* Releasing 2.0 beta 5.
diff --git a/lib/pool.py b/lib/pool.py
index 15265d8..b51d255 100644
--- a/lib/pool.py
+++ b/lib/pool.py
@@ -136,12 +136,48 @@ class SimpleConnectionPool(AbstractConnectionPool):
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):
+ """Initialize the threading lock."""
+ import threading
+ AbstractConnectionPool.__init__(
+ self, minconn, maxconn, *args, **kwargs)
+ self._lock = threading.Lock()
+
+ def getconn(self, key=None):
+ """Get a free connection and assign it to 'key' if not None."""
+ self._lock.acquire()
+ try:
+ 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:
+ 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:
+ self._closeall()
+ finally:
+ self._lock.release()
+
+
+class PersistentConnectionPool(AbstractConnectionPool):
+ """A pool that assigns persistent connections to different threads.
Note that this connection pool generates by itself the required keys
using the current thread id. This means that untill a thread put away
a connection it will always get the same connection object by successive
- .getconn() calls.
+ .getconn() calls. This also means that a thread can't use more than one
+ single connection from the pool.
"""
def __init__(self, minconn, maxconn, *args, **kwargs):
@@ -176,7 +212,7 @@ class ThreadedConnectionPool(AbstractConnectionPool):
self._lock.release()
def closeall(self):
- """Close all connections (even the one currently in use."""
+ """Close all connections (even the one currently in use.)"""
self._lock.acquire()
try:
self._closeall()