psycopg2.pool – Connections pooling¶
Creating new PostgreSQL connections can be an expensive operation. This module offers a few pure Python classes implementing simple connection pooling directly in the client application.
AbstractConnectionPool(minconn, maxconn, *args, **kwargs)¶
Base class implementing generic key-based pooling code.
New minconn connections are created automatically. The pool will support a maximum of about maxconn connections. *args and **kwargs are passed to the
The following methods are expected to be implemented by subclasses:
Get a free connection and assign it to key if not
putconn(conn, key=None, close=False)¶
Put away a connection.
If close is
True, discard the connection from the pool.
Close all the connections handled by the pool.
Note that all the connections are closed, including ones eventually in use by the application.
The following classes are
AbstractConnectionPool subclasses ready to
SimpleConnectionPool(minconn, maxconn, *args, **kwargs)¶
A connection pool that can’t be shared across different threads.
This pool class is useful only for single-threaded applications.
ThreadedConnectionPool(minconn, maxconn, *args, **kwargs)¶
A connection pool that works with the threading module.
This pool class can be safely used in multi-threaded applications.
PersistentConnectionPool(minconn, maxconn, *args, **kwargs)¶
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 until a thread puts away a connection it will always get the same connection object by successive
getconn()calls. This also means that a thread can’t use more than one single connection from the pool.
This pool class is mostly designed to interact with Zope and probably not useful in generic applications.