Consumers

Basics

The Consumer takes a connection (or channel) and a list of queues to consume from. Several consumers can be mixed to consume from different channels, as they all bind to the same connection, and drain_events will drain events from all channels on that connection.

Draining events from a single consumer:

with Consumer(connection, queues):
    connection.drain_events(timeout=1)

Draining events from several consumers:

from kombu.utils import nested

with connection.channel(), connection.channel() as (channel1, channel2):
    consumers = [Consumer(channel1, queues1),
                 Consumer(channel2, queues2)]
    with nested(\*consumers):
        connection.drain_events(timeout=1)

Or using ConsumerMixin:

from kombu.mixins import ConsumerMixin

class C(ConsumerMixin):

    def __init__(self, connection):
        self.connection = connection

    def get_consumers(self, Consumer, channel):
        return [Consumer(queues, callbacks=[self.on_message])]

    def on_message(self, body, message):
        print("RECEIVED MESSAGE: %r" % (body, ))
        message.ack()

C(connection).run()

and with multiple channels again:

from kombu.messaging import Consumer
from kombu.mixins import ConsumerMixin

class C(ConsumerMixin):
    channel2 = None

    def __init__(self, connection):
        self.connection = connection

    def get_consumers(self, _, default_channel):
        self.channel2 = default_channel.connection.channel()
        return [Consumer(default_channel, queues1,
                         callbacks=[self.on_message]),
                Consumer(self.channel2, queues2,
                         callbacks=[self.on_special_message])]

    def on_consumer_end(self, connection, default_channel):
        if self.channel2:
            self.channel2.close()

C(connection).run()

Reference

class kombu.messaging.Consumer(channel, queues=None, no_ack=None, auto_declare=None, callbacks=None, on_decode_error=None)

Message consumer.

Parameters:
  • channel – see channel.
  • queues – see queues.
  • no_ack – see no_ack.
  • auto_declare – see auto_declare
  • callbacks – see callbacks.
  • on_decode_error – see on_decode_error.
Consumer.auto_declare = True

By default all entities will be declared at instantiation, if you want to handle this manually you can set this to False.

Consumer.callbacks = None

List of callbacks called in order when a message is received.

The signature of the callbacks must take two arguments: (body, message), which is the decoded message body and the Message instance (a subclass of Message).

Consumer.cancel()

End all active queue consumers.

This does not affect already delivered messages, but it does mean the server will not send any more messages for this consumer.

Consumer.cancel_by_queue(queue)

Cancel consumer by queue name.

Consumer.channel = None

The connection/channel to use for this consumer.

Consumer.close()

End all active queue consumers.

This does not affect already delivered messages, but it does mean the server will not send any more messages for this consumer.

Consumer.declare()

Declare queues, exchanges and bindings.

This is done automatically at instantiation if auto_declare is set.

Consumer.flow(active)

Enable/disable flow from peer.

This is a simple flow-control mechanism that a peer can use to avoid overflowing its queues or otherwise finding itself receiving more messages than it can process.

The peer that receives a request to stop sending content will finish sending the current content (if any), and then wait until flow is reactivated.

Consumer.no_ack = None

Flag for message acknowledgment disabled/enabled. Enabled by default.

Consumer.on_decode_error = None

Callback called when a message can’t be decoded.

The signature of the callback must take two arguments: (message, exc), which is the message that can’t be decoded and the exception that occurred while trying to decode it.

Consumer.purge()

Purge messages from all queues.

Warning

This will delete all ready messages, there is no undo operation.

Consumer.qos(prefetch_size=0, prefetch_count=0, apply_global=False)

Specify quality of service.

The client can request that messages should be sent in advance so that when the client finishes processing a message, the following message is already held locally, rather than needing to be sent down the channel. Prefetching gives a performance improvement.

The prefetch window is Ignored if the no_ack option is set.

Parameters:
  • prefetch_size – Specify the prefetch window in octets. The server will send a message in advance if it is equal to or smaller in size than the available prefetch size (and also falls within other prefetch limits). May be set to zero, meaning “no specific limit”, although other prefetch limits may still apply.
  • prefetch_count – Specify the prefetch window in terms of whole messages.
  • apply_global – Apply new settings globally on all channels. Currently not supported by RabbitMQ.
Consumer.queues = None

A single Queue, or a list of queues to consume from.

Consumer.receive(body, message)

Method called when a message is received.

This dispatches to the registered callbacks.

Parameters:
  • body – The decoded message body.
  • message – The Message instance.
Raises NotImplementedError:
 

If no consumer callbacks have been registered.

Consumer.recover(requeue=False)

Redeliver unacknowledged messages.

Asks the broker to redeliver all unacknowledged messages on the specified channel.

Parameters:requeue – By default the messages will be redelivered to the original recipient. With requeue set to true, the server will attempt to requeue the message, potentially then delivering it to an alternative subscriber.
Consumer.register_callback(callback)

Register a new callback to be called when a message is received.

The signature of the callback needs to accept two arguments: (body, message), which is the decoded message body and the Message instance (a subclass of Message.

Consumer.revive(channel)

Revive consumer after connection loss.

Table Of Contents

Previous topic

Producers

Next topic

Examples

This Page