ConnectionPool

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

Bases: object

A pool of connections.

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

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

property broken_connections: List[Connection]

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

close()[source]

Closes all connected connections in the pool.

Return type:

None

property connected_connections: List[Connection]

List of all connected connections in the pool.

property connection_factory: Callable[[], Connection]

The factory function to create a new connection.

property connections: List[Connection]

List of all connections in the pool.

extend_pool_by_connections(connections)[source]

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

Parameters:

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

Return type:

None

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:

Connection

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.

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

static run_in_thread(func, *args, **kwargs)[source]

Method to run function in thread.

Parameters:
  • func (Callable[[ParamSpec(Param, bound= None)], Any]) – A function to run in the thread.

  • args (ParamSpecArgs) – Function args.

  • kwargs (ParamSpecKwargs) – Function kwargs.

Return type:

Thread

setup()[source]

Creates connections in the pool and connects them.

Return type:

None