Exchange

The Exchange class is used to work with RabbitMQ exchanges on an open channel. The following example shows how you can create an exchange using the rabbitpy.Exchange class.

import rabbitpy

with rabbitpy.Connection() as connection:
    with connection.channel() as channel:
        exchange = rabbitpy.Exchange(channel, 'my-exchange')
        exchange.declare()

In addition, there are four convenience classes (DirectExchange, FanoutExchange, HeadersExchange, and TopicExchange) for creating each built-in exchange type in RabbitMQ.

API Documentation

class rabbitpy.Exchange(channel, name, exchange_type='direct', durable=False, auto_delete=False, arguments=None)[source]

Exchange class for interacting with an exchange in RabbitMQ including declaration, binding and deletion.

Parameters:
  • channel (rabbitpy.channel.Channel) – The channel object to communicate on
  • name (str) – The name of the exchange
  • exchange_type (str) – The exchange type
  • durable (bool) – Request a durable exchange
  • auto_delete (bool) – Automatically delete when not in use
  • arguments (dict) – Optional key/value arguments
bind(source, routing_key=None)[source]

Bind to another exchange with the routing key.

Parameters:
  • source (str or rabbitpy.Exchange) – The exchange to bind to
  • routing_key (str) – The routing key to use
declare(passive=False)[source]

Declare the exchange with RabbitMQ. If passive is True and the command arguments do not match, the channel will be closed.

Parameters:passive (bool) – Do not actually create the exchange
delete(if_unused=False)[source]

Delete the exchange from RabbitMQ.

Parameters:if_unused (bool) – Delete only if unused
unbind(source, routing_key=None)[source]

Unbind the exchange from the source exchange with the routing key. If routing key is None, use the queue or exchange name.

Parameters:
  • source (str or rabbitpy.Exchange) – The exchange to unbind from
  • routing_key (str) – The routing key that binds them
class rabbitpy.DirectExchange(channel, name, durable=False, auto_delete=False, arguments=None)[source]

The DirectExchange class is used for interacting with direct exchanges only.

Parameters:
  • channel (rabbitpy.channel.Channel) – The channel object to communicate on
  • name (str) – The name of the exchange
  • durable (bool) – Request a durable exchange
  • auto_delete (bool) – Automatically delete when not in use
  • arguments (dict) – Optional key/value arguments
bind(source, routing_key=None)

Bind to another exchange with the routing key.

Parameters:
  • source (str or rabbitpy.Exchange) – The exchange to bind to
  • routing_key (str) – The routing key to use
declare(passive=False)

Declare the exchange with RabbitMQ. If passive is True and the command arguments do not match, the channel will be closed.

Parameters:passive (bool) – Do not actually create the exchange
delete(if_unused=False)

Delete the exchange from RabbitMQ.

Parameters:if_unused (bool) – Delete only if unused
unbind(source, routing_key=None)

Unbind the exchange from the source exchange with the routing key. If routing key is None, use the queue or exchange name.

Parameters:
  • source (str or rabbitpy.Exchange) – The exchange to unbind from
  • routing_key (str) – The routing key that binds them
class rabbitpy.FanoutExchange(channel, name, durable=False, auto_delete=False, arguments=None)[source]

The FanoutExchange class is used for interacting with fanout exchanges only.

Parameters:
  • channel (rabbitpy.channel.Channel) – The channel object to communicate on
  • name (str) – The name of the exchange
  • durable (bool) – Request a durable exchange
  • auto_delete (bool) – Automatically delete when not in use
  • arguments (dict) – Optional key/value arguments
bind(source, routing_key=None)

Bind to another exchange with the routing key.

Parameters:
  • source (str or rabbitpy.Exchange) – The exchange to bind to
  • routing_key (str) – The routing key to use
declare(passive=False)

Declare the exchange with RabbitMQ. If passive is True and the command arguments do not match, the channel will be closed.

Parameters:passive (bool) – Do not actually create the exchange
delete(if_unused=False)

Delete the exchange from RabbitMQ.

Parameters:if_unused (bool) – Delete only if unused
unbind(source, routing_key=None)

Unbind the exchange from the source exchange with the routing key. If routing key is None, use the queue or exchange name.

Parameters:
  • source (str or rabbitpy.Exchange) – The exchange to unbind from
  • routing_key (str) – The routing key that binds them
class rabbitpy.HeadersExchange(channel, name, durable=False, auto_delete=False, arguments=None)[source]

The HeadersExchange class is used for interacting with direct exchanges only.

Parameters:
  • channel (rabbitpy.channel.Channel) – The channel object to communicate on
  • name (str) – The name of the exchange
  • durable (bool) – Request a durable exchange
  • auto_delete (bool) – Automatically delete when not in use
  • arguments (dict) – Optional key/value arguments
bind(source, routing_key=None)

Bind to another exchange with the routing key.

Parameters:
  • source (str or rabbitpy.Exchange) – The exchange to bind to
  • routing_key (str) – The routing key to use
declare(passive=False)

Declare the exchange with RabbitMQ. If passive is True and the command arguments do not match, the channel will be closed.

Parameters:passive (bool) – Do not actually create the exchange
delete(if_unused=False)

Delete the exchange from RabbitMQ.

Parameters:if_unused (bool) – Delete only if unused
unbind(source, routing_key=None)

Unbind the exchange from the source exchange with the routing key. If routing key is None, use the queue or exchange name.

Parameters:
  • source (str or rabbitpy.Exchange) – The exchange to unbind from
  • routing_key (str) – The routing key that binds them
class rabbitpy.TopicExchange(channel, name, durable=False, auto_delete=False, arguments=None)[source]

The TopicExchange class is used for interacting with topic exchanges only.

Parameters:
  • channel (rabbitpy.channel.Channel) – The channel object to communicate on
  • name (str) – The name of the exchange
  • durable (bool) – Request a durable exchange
  • auto_delete (bool) – Automatically delete when not in use
  • arguments (dict) – Optional key/value arguments
bind(source, routing_key=None)

Bind to another exchange with the routing key.

Parameters:
  • source (str or rabbitpy.Exchange) – The exchange to bind to
  • routing_key (str) – The routing key to use
declare(passive=False)

Declare the exchange with RabbitMQ. If passive is True and the command arguments do not match, the channel will be closed.

Parameters:passive (bool) – Do not actually create the exchange
delete(if_unused=False)

Delete the exchange from RabbitMQ.

Parameters:if_unused (bool) – Delete only if unused
unbind(source, routing_key=None)

Unbind the exchange from the source exchange with the routing key. If routing key is None, use the queue or exchange name.

Parameters:
  • source (str or rabbitpy.Exchange) – The exchange to unbind from
  • routing_key (str) – The routing key that binds them