Simple API Methods¶
rabbitpy’s simple API methods are meant for one off use, either in your apps or in
the python interpreter. For example, if your application publishes a single
message as part of its lifetime, rabbitpy.publish()
should be enough
for almost any publishing concern. However if you are publishing more than
one message, it is not an efficient method to use as it connects and disconnects
from RabbitMQ on each invocation. rabbitpy.get()
also connects and
disconnects on each invocation. rabbitpy.consume()
does stay connected
as long as you’re iterating through the messages returned by it. Exiting the
generator will close the connection. For a more complete api, see the rabbitpy
core API.
Wrapper methods for easy access to common operations, making them both less complex and less verbose for one off or simple use cases.
- class rabbitpy.simple.SimpleChannel(uri)[source]¶
The rabbitpy.simple.Channel class creates a context manager implementation for use on a single channel where the connection is automatically created and managed for you.
Example:
import rabbitpy with rabbitpy.SimpleChannel('amqp://localhost/%2f') as channel: queue = rabbitpy.Queue(channel, 'my-queue')
- Parameters:
uri (str) – The AMQP URI to connect with. For URI options, see the
Connection
class documentation.
- rabbitpy.simple.consume(uri=None, queue_name=None, no_ack=False, prefetch=None, priority=None)[source]¶
Consume messages from the queue as a generator:
for message in rabbitpy.consume('amqp://localhost/%2F', 'my_queue'): message.ack()
- Parameters:
uri (str) – AMQP connection URI
queue_name (str) – The name of the queue to consume from
no_ack (bool) – Do not require acknowledgements
prefetch (int) – Set a prefetch count for the channel
priority (int) – Set the consumer priority
- Return type:
Iterator
- Raises:
py:class:ValueError
- rabbitpy.simple.create_direct_exchange(uri=None, exchange_name=None, durable=True)[source]¶
Create a direct exchange with RabbitMQ. This should only be used for one-off operations.
- Parameters:
uri (str) – AMQP URI to connect to
exchange_name (str) – The exchange name to create
durable (bool) – Exchange should survive server restarts
- Raises:
ValueError
- Raises:
rabbitpy.RemoteClosedException
- rabbitpy.simple.create_fanout_exchange(uri=None, exchange_name=None, durable=True)[source]¶
Create a fanout exchange with RabbitMQ. This should only be used for one-off operations.
- Parameters:
uri (str) – AMQP URI to connect to
exchange_name (str) – The exchange name to create
durable (bool) – Exchange should survive server restarts
- Raises:
ValueError
- Raises:
rabbitpy.RemoteClosedException
- rabbitpy.simple.create_headers_exchange(uri=None, exchange_name=None, durable=True)[source]¶
Create a headers exchange with RabbitMQ. This should only be used for one-off operations.
- Parameters:
uri (str) – AMQP URI to connect to
exchange_name (str) – The exchange name to create
durable (bool) – Exchange should survive server restarts
- Raises:
ValueError
- Raises:
rabbitpy.RemoteClosedException
- rabbitpy.simple.create_queue(uri=None, queue_name='', durable=True, auto_delete=False, max_length=None, message_ttl=None, expires=None, dead_letter_exchange=None, dead_letter_routing_key=None, arguments=None)[source]¶
Create a queue with RabbitMQ. This should only be used for one-off operations. If a queue name is omitted, the name will be automatically generated by RabbitMQ.
- Parameters:
uri (str) – AMQP URI to connect to
queue_name (str) – The queue name to create
durable (bool) – Indicates if the queue should survive a RabbitMQ is restart
auto_delete (bool) – Automatically delete when all consumers disconnect
max_length (int) – Maximum queue length
message_ttl (int) – Time-to-live of a message in milliseconds
expires (int) – Milliseconds until a queue is removed after becoming idle
dead_letter_exchange (str) – Dead letter exchange for rejected messages
dead_letter_routing_key (str) – Routing key for dead lettered messages
arguments (dict) – Custom arguments for the queue
- Raises:
ValueError
- Raises:
rabbitpy.RemoteClosedException
- rabbitpy.simple.create_topic_exchange(uri=None, exchange_name=None, durable=True)[source]¶
Create an exchange from RabbitMQ. This should only be used for one-off operations.
- Parameters:
uri (str) – AMQP URI to connect to
exchange_name (str) – The exchange name to create
durable (bool) – Exchange should survive server restarts
- Raises:
ValueError
- Raises:
rabbitpy.RemoteClosedException
- rabbitpy.simple.delete_exchange(uri=None, exchange_name=None)[source]¶
Delete an exchange from RabbitMQ. This should only be used for one-off operations.
- Parameters:
uri (str) – AMQP URI to connect to
exchange_name (str) – The exchange name to delete
- Raises:
ValueError
- Raises:
rabbitpy.RemoteClosedException
- rabbitpy.simple.delete_queue(uri=None, queue_name=None)[source]¶
Delete a queue from RabbitMQ. This should only be used for one-off operations.
- Parameters:
uri (str) – AMQP URI to connect to
queue_name (str) – The queue name to delete
- Return type:
bool
- Raises:
ValueError
- Raises:
rabbitpy.RemoteClosedException
- rabbitpy.simple.get(uri=None, queue_name=None)[source]¶
Get a message from RabbitMQ, auto-acknowledging with RabbitMQ if one is returned.
Invoke directly as
rabbitpy.get()
- Parameters:
uri (str) – AMQP URI to connect to
queue_name (str) – The queue name to get the message from
- Return type:
py:class:rabbitpy.message.Message or None
- Raises:
py:class:ValueError
- rabbitpy.simple.publish(uri=None, exchange_name=None, routing_key=None, body=None, properties=None, confirm=False)[source]¶
Publish a message to RabbitMQ. This should only be used for one-off publishing, as you will suffer a performance penalty if you use it repeatedly instead creating a connection and channel and publishing on that
- Parameters:
uri (str) – AMQP URI to connect to
exchange_name (str) – The exchange to publish to
routing_key (str) – The routing_key to publish with
body (str or unicode or bytes or dict or list) – The message body
properties (dict) – Dict representation of Basic.Properties
confirm (bool) – Confirm this delivery with Publisher Confirms
- Return type:
bool or None