AsyncConnectionPool

class zcached.AsyncConnectionPool(pool_size, connection_factory)[source]

Bases: object

A pool of asynchronous connections.

Parameters:
  • pool_size (int) – The maximum size of the connection pool.

  • connection_factory (Callable[[], AsyncConnection]) – A callable that returns a new instance of AsyncConnection.

property broken_connections: List[AsyncConnection]

List of all broken (not connected) connections in the pool.

cleanup_broken_connections()[source]

Closes broken connections and removes them from the list of connections.

Return type:

None

async close()[source]

Closes all connected connections in the pool.

Return type:

None

property connected_connections: List[AsyncConnection]

List of all connected connections in the pool.

property connection_factory: Callable[[], AsyncConnection]

The factory function to create a new connection.

property connections: List[AsyncConnection]

List of all connections in the pool.

async extend_pool_by_connections(connections)[source]

Extends the pool with existing connections. The pool size will be increased if necessary.

Parameters:

connections (Iterable[AsyncConnection]) – An iterable of existing connections to add to the pool.

Return type:

None

async extend_pool_by_size(size)[source]

Extends the pool by a specified number of connections. The pool size will be increased if necessary.

Parameters:

size (int) – The number of connections to add to the pool.

Return type:

None

get_least_loaded_connection()[source]

Get the least loaded connection from the pool. Only working connections are considered.

Raises:

IndexError – If the pool is empty.

Return type:

AsyncConnection

is_empty()[source]

True if the pool is empty.

Return type:

bool

is_full()[source]

True if the pool is full.

Return type:

bool

is_working()[source]

True if there is any working connection in the pool.

Return type:

bool

property pool_size: int

The maximum size of the connection pool.

async reconnect(only_broken_connections=True)[source]

Attempts to reconnect connections in the pool. Returns the number of connected connections after reconnection.

Parameters:

only_broken_connections (bool) – If True, attempts to reconnect only broken connections.

Return type:

int

reduce_pool_connections(amount, delete_pending_connections=False)[source]

Reduces the size of the connection pool by a specified amount. By default, the method first removes non-working connections, when this is not enough it takes connections that have 0 pending requests and removes them too. If a connection has any pending requests, it doesn’t delete it, well, unless the delete_pending_connections parameter is on True.

Parameters:
  • amount (int) – The number of connections to remove from the pool.

  • delete_pending_connections (bool) – If True, the method will also delete, when necessary, connections that have pending requests. This is not strongly recommended!

Return type:

None

async setup()[source]

Creates connections in the pool and connects them.

Return type:

None