Channel

A Channel is created on an active connection using the Connection.channel() method. Channels can act as normal Python objects:

conn = rabbitpy.Connection()
chan = conn.channel()
chan.enable_publisher_confirms()
chan.close()

or as a Python context manager (See PEP 0343):

with rabbitpy.Connection() as conn:
    with conn.channel() as chan:
        chan.enable_publisher_confirms()

When they are used as a context manager with the with statement, when your code exits the block, the channel will automatically close, issuing a clean shutdown with RabbitMQ via the Channel.Close RPC request.

You should be aware that if you perform actions on a channel with exchanges, queues, messages or transactions that RabbitMQ does not like, it will close the channel by sending an AMQP Channel.Close RPC request to your application. Upon receipt of such a request, rabbitpy will raise the appropriate exception referenced in the request.

API Documentation

class rabbitpy.Channel(channel_id, server_capabilities, events, exception_queue, read_queue, write_queue, maximum_frame_size, write_trigger, blocking_read=False)[source]

The Channel object is the communications object used by Exchanges, Messages, Queues, and Transactions. It is created by invoking the rabbitpy.Connection.channel() method. It can act as a context manager, allowing for quick shorthand use:

with connection.channel():
   # Do something

To create a new channel, invoke py:meth:rabbitpy.connection.Connection.channel

To improve performance, pass blocking_read to True. Note that doing so prevents KeyboardInterrupt/CTRL-C from exiting the Python interpreter.

Parameters:
  • channel_id (int) – The channel # to use for this instance
  • server_capabilities (dict) – Features the server supports
  • rabbitpy.Events (events) – Event management object
  • exception_queue (queue.Queue) – Exception queue
  • read_queue (queue.Queue) – Queue to read pending frames from
  • write_queue (queue.Queue) – Queue to write pending AMQP objs to
  • maximum_frame_size (int) – The max frame size for msg bodies
  • write_trigger (socket) – Write to this socket to break IO waiting
  • blocking_read (bool) – Use blocking Queue.get to improve performance
Raises:

rabbitpy.exceptions.RemoteClosedChannelException

Raises:

rabbitpy.exceptions.AMQPException

close()[source]

Close the channel, cancelling any active consumers, purging the read queue, while looking to see if a Basic.Nack should be sent, sending it if so.

enable_publisher_confirms()[source]

Turn on Publisher Confirms. If confirms are turned on, the Message.publish command will return a bool indicating if a message has been successfully published.

id

Return the channel id

Return type:int
open()[source]

Open the channel, invoked directly upon creation by the Connection

prefetch_count(value, all_channels=False)[source]

Set a prefetch count for the channel (or all channels on the same connection).

Parameters:
  • value (int) – The prefetch count to set
  • all_channels (bool) – Set the prefetch count on all channels on the same connection
prefetch_size(value, all_channels=False)[source]

Set a prefetch size in bytes for the channel (or all channels on the same connection).

Parameters:
  • value (int) – The prefetch size to set
  • all_channels (bool) – Set the prefetch size on all channels on the same connection
publisher_confirms

Returns True if publisher confirms are enabled.

Return type:bool
recover(requeue=False)[source]

Recover all unacknowledged messages that are associated with this channel.

Parameters:requeue (bool) – Requeue the message