AsyncConnection

class zcached.AsyncConnection(host, port, connection_attempts=3, reconnect=True, timeout_limit=15, buffer_size=2048, loop=None, protocol_type=None)[source]

Bases: Connection, Generic[ProtocolT]

An asynchronous connection object to manage communication with the server.

Parameters:
  • host (str) – The host address of the server to connect to.

  • port (int) – The port number of the server to connect to.

  • connection_attempts (int) – The maximum number of attempts to establish a connection with the server. If the maximum number of attempts is exceeded, an error will be raised.

  • reconnect (bool) – Flag indicating whether automatic reconnection attempt should be made in case of a broken connection.

  • timeout_limit (int) – The maximum time in seconds to wait for a response from the server.

  • buffer_size (int) – The size of the buffer for receiving data from the server, in bytes. Larger values for buffer_size may allow for more data to be received in a single operation, while smaller values can be more memory-efficient but slower.

  • loop (AbstractEventLoop | None) – The event loop to run asynchronous tasks. If None, the default event loop will be used.

  • protocol_type (Optional[Type[TypeVar(ProtocolT, bound= StreamReaderProtocol)]]) – The protocol type which is used to building protocol for managing the connection.

connection_attempts

The maximum number of attempts to establish a connection with the server.

reconnect

Flag indicating whether automatic reconnection attempt should be made in case of a broken connection.

timeout_limit

The maximum time in seconds to wait for a response from the server.

buffer_size

The size of the buffer for receiving data from the server, in bytes.

loop

The event loop to run asynchronous tasks.

async close()[source]

Closes the connection by closing the writer, and waiting until the writer is fully closed.

Return type:

None

async connect()[source]

Coroutine to establish a connection with the server asynchronously.

Return type:

None

async open_connection(host, port, **kwargs)[source]

Coroutine to open a connection to the server.

Parameters:
  • host (str) – The host address of the server.

  • port (int) – The port number of the server.

  • **kwargs (Any) – Additional keyword arguments to pass to the connection setup.

Return type:

tuple[StreamReader, StreamWriter]

property protocol: ProtocolT | None

The protocol for managing the connection. If available.

property protocol_type: Type[ProtocolT]

The type of protocol used for managing the connection.

property reader: StreamReader | None

The asyncio.StreamReader object for reading data from the server. If available.

async receive(timeout_limit=None)[source]

Coroutine to receive data from the reader.

NOT TASK SAFE.

Parameters:

timeout_limit (float | None) – The maximum time in seconds to wait for a response from the server.

Raises:

asyncio.TimeoutError – If the timeout limit has been exceeded.

Return type:

bytes | None

async send(data)[source]

Coroutine to send a data to the server.

TASK SAFE.

Parameters:

data (bytes) – Bytes to send.

Return type:

Result

property transport: None | WriteTransport

The transport object for the connection, if StreamWriter is available.

async try_reconnect()[source]

A method to attempt to reconnect to the server if the connection is broken.

Return type:

Result[bytes]

async wait_for_response()[source]

Coroutine to wait for a complete response from the server asynchronously.

NOT TASK SAFE.

Return type:

Result

property writer: StreamWriter | None

The asyncio.StreamWriter object for writing data to the server. If available.