pikachewie.broker – A RabbitMQ broker

class pikachewie.broker.Broker(nodes=None, connect_options=None)

A RabbitMQ broker.

nodes is a two-level dict containing the names and network locations of the clustered nodes that comprise the RabbitMQ broker, e.g.:

{
    'bunny1': {'host': 'localhost'},
    'bunny2': {'host': 'rabbit.example.com', 'port': 5678}
}

Valid netloc parameters are host and port. If not specified, nodes defaults to {'default': {}}, which represents a broker consisting of one node named “default” located at localhost:5672.

connect_options is a dict of keywords arguments to be passed to pika.connection.ConnectionParameters. If not specified, the pika default connection parameters will be used. For the list of supported keyword arguments and their default values, see pika.connection.ConnectionParameters.

Parameters:
connect(on_open_callback=None, stop_ioloop_on_close=False, connection_attempts=<object object>, on_failure_callback=None, cycle_delay=None, blocking=False)

Return a new connection to this broker.

This method connects to each node of the broker in turn, until either a connection is successfully established, or the number of total connection_attempts is reached. The order in which the nodes are tried is random, and potentially different between different calls to connect().

If connection_attempts is greater than the number of broker nodes, this method cycles through the list of nodes repeatedly as needed, pausing for cycle_delay seconds between each pass through the node list.

If connection_attempts is not specified, it defaults to the number of broker nodes, so that each node is tried at most once. If connection_attempts is explicitly set to None, there is no limit on the number of connection attempts, and connect() blocks until a connection is successfully made.

When connection_attempts is reached, the resultant behavior depends on whether an on_failure_callback has been specified. If it is defined, the on_failure_callback is called with a BrokerConnectionError as its sole argument, and connect() returns None. Otherwise, connect() raises the BrokerConnectionError.

Parameters:
  • on_open_callback (callable) – invoked once the AMQP connection is opened. Note: this parameter is ignored if blocking is True.
  • stop_ioloop_on_close (bool) – whether to stop the IOLoop when the connection is closed (default: True). Note: this parameter is ignored if blocking is True.
  • connection_attempts (int or NoneType) – maximum numbers of total connection attempts to make before failing
  • on_failure_callback (callable) – invoked if the connection attempt fails
  • cycle_delay (float or NoneType) – number of seconds to sleep between each cycle through the nodes list
  • blocking (bool) – if True, return a pika.BlockingConnection (default: False)
Returns:

new connection to this broker (or None)

Return type:

pika.adapters.tornado_connection.TornadoConnection or pika.BlockingConnection or NoneType

Raises:

BrokerConnectionError

exception pikachewie.broker.BrokerConnectionError

Raised when a new connection cannot be opened to a Broker.