kombu.messaging

Sending and receiving messages.

copyright:
  1. 2009 - 2012 by Ask Solem.
license:

BSD, see LICENSE for more details.

Message Producer

class kombu.messaging.Producer(channel, exchange=None, routing_key=None, serializer=None, auto_declare=None, compression=None, on_return=None)

Message Producer.

Parameters:
  • channel – Connection or channel.
  • exchange – Optional default exchange.
  • routing_key – Optional default routing key.
  • serializer – Default serializer. Default is “json”.
  • compression – Default compression method. Default is no compression.
  • auto_declare – Automatically declare the default exchange at instantiation. Default is True.
  • on_return – Callback to call for undeliverable messages, when the mandatory or immediate arguments to publish() is used. This callback needs the following signature: (exception, exchange, routing_key, message). Note that the producer needs to drain events to use this feature.
channel = None

The connection channel used.

exchange = None

Default exchange.

routing_key = ''
serializer = None

Default serializer to use. Default is JSON.

compression = None

Default compression method. Disabled by default.

auto_declare = True

By default the exchange is declared at instantiation. If you want to declare manually then you can set this to False.

on_return = None

Basic return callback.

declare()

Declare the exchange.

This happens automatically at instantiation if auto_declare is enabled.

publish(body, routing_key=None, delivery_mode=None, mandatory=False, immediate=False, priority=0, content_type=None, content_encoding=None, serializer=None, headers=None, compression=None, exchange=None, retry=False, retry_policy=None, declare=[], **properties)

Publish message to the specified exchange.

Parameters:
  • body – Message body.
  • routing_key – Message routing key.
  • delivery_mode – See delivery_mode.
  • mandatory – Currently not supported.
  • immediate – Currently not supported.
  • priority – Message priority. A number between 0 and 9.
  • content_type – Content type. Default is auto-detect.
  • content_encoding – Content encoding. Default is auto-detect.
  • serializer – Serializer to use. Default is auto-detect.
  • compression – Compression method to use. Default is none.
  • headers – Mapping of arbitrary headers to pass along with the message body.
  • exchange – Override the exchange. Note that this exchange must have been declared.
  • declare – Optional list of required entities that must have been declared before publishing the message. The entities will be declared using maybe_declare().
  • retry – Retry publishing, or declaring entities if the connection is lost.
  • retry_policy – Retry configuration, this is the keywords supported by ensure().
  • **properties – Additional message properties, see AMQP spec.
revive(channel)

Revive the producer after connection loss.

Message Consumer

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

Message consumer.

Parameters:
channel = None

The connection/channel to use for this consumer.

queues = None

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

no_ack = None

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

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.

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).

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.

declare()

Declare queues, exchanges and bindings.

This is done automatically at instantiation if auto_declare is set.

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.

consume(no_ack=None)
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.

cancel_by_queue(queue)

Cancel consumer by queue name.

purge()

Purge messages from all queues.

Warning

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

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.

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.
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.
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.

revive(channel)

Revive consumer after connection loss.

Table Of Contents

Previous topic

kombu.simple

Next topic

kombu.entity

This Page