The `!ConnectionPool` class
---------------------------
-.. autoclass:: ConnectionPool(conninfo, *, **arguments)
+.. autoclass:: ConnectionPool
This class implements a connection pool serving `~psycopg.Connection`
instances (or subclasses). The constructor has *alot* of arguments, but
be a `!Connection` subclass.
:type connection_class: `!type`, default: `~psycopg.Connection`
+ :param open: If `!true`, open the pool, creating the required connections,
+ on init. If `!false`, open the pool when `!open()` is called or
+ when the pool context is entered. See the `open()` method
+ documentation for more details.
+ :type open: `!bool`, default: `!true`
+
:param configure: A callback to configure a connection after creation.
Useful, for instance, to configure its adapters. If the
connection is used to run internal queries (to inspect the
the connection attempt is aborted and the
*reconnect_failed* callback invoked.
:type reconnect_timeout: `!float`, default: 5 minutes
-
+
:param reconnect_failed: Callback invoked if an attempt to create a new
connection fails for more than *reconnect_timeout*
seconds. The user may decide, for instance, to
they are returned to the pool.
:type num_workers: `!int`, default: 3
+ .. versionchanged:: psycopg_pool 3.1
+
+ Added `!open` parameter to init method.
+
+ .. note:: In a future version, the deafult value for the `!open` parameter
+ might be changed to `!false`. If you rely on this behaviour (e.g. if
+ you don't use the pool as a context manager) you might want to specify
+ this parameter explicitly.
+
.. automethod:: wait
.. automethod:: connection
-
+
.. code:: python
with my_pool.connection() as conn:
conn.execute(...)
# the connection is now back in the pool
-
+
.. automethod:: open
+ .. versionadded:: psycopg_pool 3.1
+
+
.. automethod:: close
- .. note::
-
- The pool can be used as context manager too, in which case it will
- be closed at the end of the block:
+ .. note::
- .. code:: python
+ The pool can be also used as a context manager, in which case it will
+ be opened (if necessary) on entering the block and closed on exiting it:
- with ConnectionPool(...) as pool:
- # code using the pool
+ .. code:: python
+
+ with ConnectionPool(...) as pool:
+ # code using the pool
.. attribute:: name
:type: str
The current minimum and maximum size of the pool. Use `resize()` to
change them at runtime.
-
+
.. automethod:: resize
.. automethod:: check
.. automethod:: get_stats
.. automethod:: getconn
.. automethod:: putconn
+
Pool exceptions
---------------
Only the function with different signature from `!ConnectionPool` are
listed here.
-.. autoclass:: AsyncConnectionPool(conninfo, *, **arguments)
+.. autoclass:: AsyncConnectionPool
All the other parameters are the same.
.. automethod:: wait
.. automethod:: connection
-
+
.. code:: python
async with my_pool.connection() as conn:
await conn.execute(...)
# the connection is now back in the pool
-
+
.. automethod:: open
.. automethod:: close
- .. note::
-
- The pool can be used as context manager too, in which case it will
- be closed at the end of the block:
+ .. note::
- .. code:: python
+ The pool can be also used as an async context manager, in which case it
+ will be opened (if necessary) on entering the block and closed on
+ exiting it:
+
+ .. code:: python
- async with AsyncConnectionPool(...) as pool:
- # code using the pool
+ async with AsyncConnectionPool(...) as pool:
+ # code using the pool
.. automethod:: resize
.. automethod:: check
:ref:`connection context behaviour <with-connection>` (commit/rollback
the transaction in case of success/error). If the connection is no more
in working state replace it with a new one.
-
"""
conn = self.getconn(timeout=timeout)
t0 = monotonic()
self._return_connection(conn)
def open(self) -> None:
- """Open the pool by starting worker threads.
+ """Open the pool by starting connecting and and accepting clients.
- No-op if the pool is already opened.
+ The method is no-op if the pool is already opened (because the method
+ was already called, or because the pool context was entered, or because
+ the pool was initialized with ``open=true``.
"""
with self._lock:
if not self._closed:
def close(self, timeout: float = 5.0) -> None:
"""Close the pool and make it unavailable to new clients.
- All the waiting and future client will fail to acquire a connection
+ All the waiting and future clients will fail to acquire a connection
with a `PoolClosed` exception. Currently used connections will not be
closed until returned to the pool.