Various pool changes.

This commit is contained in:
Federico Di Gregorio 2005-10-19 14:38:37 +00:00
parent d5f639cfd6
commit f687f2853e
2 changed files with 45 additions and 3 deletions

View File

@ -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> 2005-10-19 Federico Di Gregorio <fog@initd.org>
* Releasing 2.0 beta 5. * Releasing 2.0 beta 5.

View File

@ -136,12 +136,48 @@ class SimpleConnectionPool(AbstractConnectionPool):
class ThreadedConnectionPool(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 Note that this connection pool generates by itself the required keys
using the current thread id. This means that untill a thread put away 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 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): def __init__(self, minconn, maxconn, *args, **kwargs):
@ -176,7 +212,7 @@ class ThreadedConnectionPool(AbstractConnectionPool):
self._lock.release() 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() self._lock.acquire()
try: try:
self._closeall() self._closeall()