Broker Connections - kombu.connection

class kombu.connection.BrokerConnection(hostname='localhost', userid='guest', password='guest', virtual_host='/', port=None, insist=False, ssl=False, transport=None, connect_timeout=5, pool=None, backend_cls=None)

A connection to the broker.

Parameters:
  • hostname – Hostname/address of the server to connect to. Default is "localhost".
  • userid – Username. Default is "guest".
  • password – Password. Default is "guest".
  • virtual_host – Virtual host. Default is "/".
  • port – Port of the server. Default is transport specific.
  • insist – Insist on connecting to a server. In a configuration with multiple load-sharing servers, the insist option tells the server that the client is insisting on a connection to the specified server. Default is False.
  • ssl – Use ssl to connect to the server. Default is False.
  • transport – Transport class to use. Can be a class, or a string specifying the path to the class. (e.g. kombu.transport.pyamqplib.Transport), or one of the aliases: amqplib, pika, redis, memory.
  • connect_timeout – Timeout in seconds for connecting to the server. May not be suported by the specified transport.

Usage

Creating a connection:

>>> conn = BrokerConnection("rabbit.example.com")

The connection is established lazily when needed. If you need the connection to be established, then do so expliclty using connect():

>>> conn.connect()

Remember to always close the connection:

>>> conn.release()
SimpleBuffer(name, no_ack=False, queue_opts=None, exchange_opts=None, channel=None)

Create new SimpleQueue using a channel from this connection.

Same as SimpleQueue(), but configured with buffering semantics. The resulting queue and exchange will not be durable, also auto delete is enabled. Messages will be transient (not persistent), and acknowledgements are disabled (no_ack).

SimpleQueue(name, no_ack=False, queue_opts=None, exchange_opts=None, channel=None)

Create new SimpleQueue, using a channel from this connection.

If name is a string, a queue and exchange will be automatically created using that name as the name of the queue and exchange, also it will be used as the default routing key.

Parameters:
  • name – Name of the queue/or a Queue.
  • no_ack – Disable acknowledgements. Default is false.
  • queue_opts – Additional keyword arguments passed to the constructor of the automatically created Queue.
  • exchange_opts – Additional keyword arguments passed to the constructor of the automatically created Exchange.
  • channel – Channel to use. If not specified a new channel from the current connection will be used. Remember to call close() when done with the object.
acquire()

Acquire connection.

Only here for API compatibility with BrokerConnectionPool.

channel()

Request a new channel.

channel_errors

List of exceptions that may be raised by the channel.

clone(**kwargs)

Create a copy of the connection with the same connection settings.

close()

Close the connection (if open).

connect()

Establish connection to server immediately.

connection
connection_errors

List of exceptions that may be raised by the connection.

create_backend()
create_transport()
drain_events(**kwargs)

Wait for a single event from the server.

Parameters:
  • timeout – Timeout in seconds before we give up. Raises socket.timeout if the timeout is execeded.

Usually used from an event loop.

ensure(fun, errback=None, max_retries=None, interval_start=1, interval_step=1, interval_max=1)

Ensure operation completes, regardless of any channel/connection errors occuring.

Will retry by establishing the connection, and reapplying the function.

Parameters:
  • fun – Method to apply.
  • errback – Optional callback called each time the connection can’t be established. Arguments provided are the exception raised and the interval that will be slept (exc, interval).
  • max_retries – Maximum number of times to retry. If this limit is exceeded the connection error will be re-raised.
  • interval_start – The number of seconds we start sleeping for.
  • interval_step – How many seconds added to the interval for each retry.
  • interval_max – Maximum number of seconds to sleep between each retry.

Example

This is an example ensuring a publish operation:

>>> def errback(exc, interval):
...     print("Couldn't publish message: %r. Retry in %ds" % (
...             exc, interval))
>>> publish = conn.ensure(producer.publish,
...                       errback=errback, max_retries=3)
>>> publish(message, routing_key)
ensure_connection(errback=None, max_retries=None, interval_start=2, interval_step=2, interval_max=30)

Ensure we have a connection to the server.

If not retry establishing the connection with the settings specified.

Parameters:
  • errback – Optional callback called each time the connection can’t be established. Arguments provided are the exception raised and the interval that will be slept (exc, interval).
  • max_retries – Maximum number of times to retry. If this limit is exceeded the connection error will be re-raised.
  • interval_start – The number of seconds we start sleeping for.
  • interval_step – How many seconds added to the interval for each retry.
  • interval_max – Maximum number of seconds to sleep between each retry.
get_transport_cls()

Get the currently used transport class.

host

The host as a hostname/port pair separated by colon.

info()

Get connection info.

release()

Close the connection, or if the connection is managed by a pool the connection will be released to the pool so it can be reused.

NOTE: You must never perform operations on a connection that has been released.

transport
class kombu.connection.BrokerConnectionPool(initial, max=10, ensure=False, preconnect=0)

Pool of connections.

Parameters:
  • initial – Initial BrokerConnection to take connection parameters from.
  • max – Maximum number of connections in the pool. Default is 10.
  • ensure – When preconnect on, ensure we’re able to establish a connection. Default is False.
  • preconnect – Number of connections at instantiation. Default is to only establish connections when needed.
acquire(block=True, timeout=None)

Acquire connection.

Raises kombu.exceptions.PoolExhausted:
 If there are no available connections to be acquired.
active

Number of acquired connections.

close()

Close all connections.

ensure(fun, errback=None, max_retries=None, interval_start=2, interval_step=2, interval_max=30)

See BrokerConnection.ensure().

grow(n, connect=False)

Add n more connections to the pool.

Parameters:
  • connect – Establish connections imemediately. By default connections are only established when needed.
Raises kombu.exceptions.PoolLimitExceeded:
 

If there are already more than max number of connections in the pool.

release(connection)

Release connection so it can be used by others.

NOTE: You must never perform operations on a connection that has been released.

replace(connection)

Clone and replace connection with a new one.

This is useful if the connection is broken.

total

Current total number of connections

Previous topic

API Reference

Next topic

Simple Interface - kombu.simple

This Page