Connection

rabbitpy Connection objects are used to connect to RabbitMQ. They provide a thread-safe connection to RabbitMQ that is used to authenticate and send all channel based RPC commands over. Connections use AMQP URI syntax for specifying the all of the connection information, including any connection negotiation options, such as the heartbeat interval. For more information on the various query parameters that can be specified, see the official documentation.

A Connection is a normal python object that you use:

conn = rabbitpy.Connection('amqp://guest:guest@localhost:5672/%2F')
conn.close()

or it can be used as a Python context manager (See PEP 0343):

with rabbitpy.Connection() as conn:
    # Foo

When it is used as a context manager with the with statement, when your code exits the block, the connection will automatically close.

If RabbitMQ remotely closes your connection via the AMQP Connection.Close RPC request, rabbitpy will raise the appropriate exception referenced in the request.

If heartbeats are enabled (default: 5 minutes) and RabbitMQ does not send a heartbeat request in >= 2 heartbeat intervals, a ConnectionResetException will be raised.

API Documentation

class rabbitpy.Connection(url=None)[source]

The Connection object is responsible for negotiating a connection and managing its state. When creating a new instance of the Connection object, if no URL is passed in, it uses the default connection parameters of localhost port 5672, virtual host / with the guest/guest username/password combination. Represented as a AMQP URL the connection information is:

amqp://guest:guest@localhost:5672/%2F

To use a different connection, pass in a AMQP URL that follows the standard format:

[scheme]://[username]:[password]@[host]:[port]/[virtual_host]

The following example connects to the test virtual host on a RabbitMQ server running at 192.168.1.200 port 5672 as the user “www” and the password rabbitmq:

amqp://admin192.168.1.200:5672/test

Note

You should be aware that most connection exceptions may be raised during the use of all functionality in the library.

Parameters:url (str) – The AMQP connection URL
Raises:rabbitpy.exceptions.AMQPException
Raises:rabbitpy.exceptions.ConnectionException
Raises:rabbitpy.exceptions.ConnectionResetException
Raises:rabbitpy.exceptions.RemoteClosedException
blocked

Indicates if the connection is blocked from publishing by RabbitMQ.

This flag indicates communication from RabbitMQ that the connection is blocked using the Connection.Blocked RPC notification from RabbitMQ that was added in RabbitMQ 3.2.

@TODO If RabbitMQ version < 3.2, use the HTTP management API to query the value

Return type:bool
capabilities

Return the RabbitMQ Server capabilities from the connection negotiation process.

Return type:dict
channel(blocking_read=False)[source]

Create a new channel

If blocking_read is True, the cross-thread Queue.get use will use blocking operations that lower resource utilization and increase throughput. However, due to how Python’s blocking Queue.get is implemented, KeyboardInterrupt is not raised when CTRL-C is pressed.

Parameters:blocking_read (bool) – Enable for higher throughput
Raises:rabbitpy.exceptions.AMQPException
Raises:rabbitpy.exceptions.RemoteClosedChannelException
close()[source]

Close the connection, including all open channels

server_properties

Return the RabbitMQ Server properties from the connection negotiation process.

Return type:dict