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 all of the connection information, including any connection negotiation options, such as the heartbeat interval.
Usage
A Connection can be used as a normal Python object:
Or as a Python context manager:
When used as a context manager, the connection is automatically closed when the block exits.
If RabbitMQ remotely closes the connection, rabbitpy will raise the appropriate exception (see Exceptions).
If heartbeats are enabled (default: 5 minutes) and RabbitMQ does not send a
heartbeat in >= 2 heartbeat intervals, a ConnectionResetException will be raised.
Connection URL Parameters
The connection URL follows standard AMQP URI syntax:
Common query parameters:
| Parameter | Default | Description |
|---|---|---|
heartbeat |
300 |
Heartbeat interval in seconds |
channel_max |
0 |
Maximum number of channels |
frame_max |
131072 |
Maximum frame size in bytes |
API
rabbitpy.connection.Connection
Bases: StatefulBase
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:
:code:`amqp://guest:guest@localhost:5672/%2F`
To use a different connection, pass in a AMQP URL that follows the standard format:
:code:`[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:
:code:`amqp://www:rabbitmq@192.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.
:param url: The AMQP connection URL :param connection_name: An optional name for the connection :param client_properties: Optional client properties to send :raises: exceptions.AMQPException :raises: exceptions.ConnectionException :raises: exceptions.ConnectionResetException :raises: exceptions.RemoteClosedException
args
property
Return the connection arguments.
blocked
property
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.
capabilities
property
Return the RabbitMQ server capabilities from negotiation.
server_properties
property
Return the RabbitMQ server properties from negotiation.
__enter__()
For use as a context manager, return a handle to this object instance.
__exit__(exc_type, exc_val, unused_exc_tb)
Close the connection when the context exits
__init__(url=None, connection_name=None, client_properties=None)
Create a new instance of the Connection object and connect.
channel(blocking_read=False)
Create and return a new AMQP channel.
If blocking_read is True, :py:meth:Queue.get calls within
the channel use blocking operations which lower CPU usage and increase
throughput. However, KeyboardInterrupt will not be raised while
the channel is blocked waiting for a frame.
:param blocking_read: Enable blocking reads for higher throughput :raises: exceptions.ConnectionClosed :raises: exceptions.TooManyChannelsError
close()
Close the connection to RabbitMQ, including all open channels.
connect()
Connect to RabbitMQ and negotiate the AMQP connection.