Connection

The Connection class implements the base behavior that all connection adapters extend.

class pika.connection.Connection(parameters=None, on_open_callback=None, on_open_error_callback=None, on_close_callback=None, internal_connection_workflow=True)[source]

This is the core class that implements communication with RabbitMQ. This class should not be invoked directly but rather through the use of an adapter such as SelectConnection or BlockingConnection.

add_on_close_callback(callback)[source]

Add a callback notification when the connection has closed. The callback will be passed the connection and an exception instance. The exception will either be an instance of exceptions.ConnectionClosed if a fully-open connection was closed by user or broker or exception of another type that describes the cause of connection closure/failure.

Parameters

callback (callable) – Callback to call on close, having the signature: callback(pika.connection.Connection, exception)

add_on_connection_blocked_callback(callback)[source]

RabbitMQ AMQP extension - Add a callback to be notified when the connection gets blocked (Connection.Blocked received from RabbitMQ) due to the broker running low on resources (memory or disk). In this state RabbitMQ suspends processing incoming data until the connection is unblocked, so it’s a good idea for publishers receiving this notification to suspend publishing until the connection becomes unblocked.

See also Connection.add_on_connection_unblocked_callback()

See also ConnectionParameters.blocked_connection_timeout.

Parameters

callback (callable) – Callback to call on Connection.Blocked, having the signature callback(connection, pika.frame.Method), where the method frame’s method member is of type pika.spec.Connection.Blocked

add_on_connection_unblocked_callback(callback)[source]

RabbitMQ AMQP extension - Add a callback to be notified when the connection gets unblocked (Connection.Unblocked frame is received from RabbitMQ) letting publishers know it’s ok to start publishing again.

Parameters

callback (callable) – Callback to call on Connection.Unblocked, having the signature callback(connection, pika.frame.Method), where the method frame’s method member is of type pika.spec.Connection.Unblocked

add_on_open_callback(callback)[source]

Add a callback notification when the connection has opened. The callback will be passed the connection instance as its only arg.

Parameters

callback (callable) – Callback to call when open

add_on_open_error_callback(callback, remove_default=True)[source]

Add a callback notification when the connection can not be opened.

The callback method should accept the connection instance that could not connect, and either a string or an exception as its second arg.

Parameters
  • callback (callable) – Callback to call when can’t connect, having the signature _(Connection, Exception)

  • remove_default (bool) – Remove default exception raising callback

property basic_nack

Specifies if the server supports basic.nack on the active connection.

Return type

bool

channel(channel_number=None, on_open_callback=None)[source]

Create a new channel with the next available channel number or pass in a channel number to use. Must be non-zero if you would like to specify but it is recommended that you let Pika manage the channel numbers.

Parameters
  • channel_number (int) – The channel number to use, defaults to the next available.

  • on_open_callback (callable) – The callback when the channel is opened. The callback will be invoked with the Channel instance as its only argument.

Return type

pika.channel.Channel

close(reply_code=200, reply_text='Normal shutdown')[source]

Disconnect from RabbitMQ. If there are any open channels, it will attempt to close them prior to fully disconnecting. Channels which have active consumers will attempt to send a Basic.Cancel to RabbitMQ to cleanly stop the delivery of messages prior to closing the channel.

Parameters
  • reply_code (int) – The code number for the close

  • reply_text (str) – The text reason for the close

Raises

pika.exceptions.ConnectionWrongStateError – if connection is closed or closing.

property consumer_cancel_notify

Specifies if the server supports consumer cancel notification on the active connection.

Return type

bool

property exchange_exchange_bindings

Specifies if the active connection supports exchange to exchange bindings.

Return type

bool

property is_closed

Returns a boolean reporting the current connection state.

property is_closing

Returns True if connection is in the process of closing due to client-initiated close request, but closing is not yet complete.

property is_open

Returns a boolean reporting the current connection state.

property publisher_confirms

Specifies if the active connection can use publisher confirmations.

Return type

bool