This document describes the current stable version of Kombu (5.3). For development docs, go here.

Mixin Classes - kombu.mixins


class kombu.mixins.ConsumerMixin[source]

Convenience mixin for implementing consumer programs.

It can be used outside of threads, with threads, or greenthreads (eventlet/gevent) too.

The basic class would need a connection attribute which must be a Connection instance, and define a get_consumers() method that returns a list of kombu.Consumer instances to use. Supporting multiple consumers is important so that multiple channels can be used for different QoS requirements.


class Worker(ConsumerMixin):
    task_queue = Queue('tasks', Exchange('tasks'), 'tasks')

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

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

    def on_task(self, body, message):
        print('Got task: {0!r}'.format(body))
\* :meth:`extra_context`

Optional extra context manager that will be entered after the connection and consumers have been set up.

Takes arguments (connection, channel).

\* :meth:`on_connection_error`

Handler called if the connection is lost/ or is unavailable.

Takes arguments (exc, interval), where interval is the time in seconds when the connection will be retried.

The default handler will log the exception.

\* :meth:`on_connection_revived`

Handler called as soon as the connection is re-established after connection failure.

Takes no arguments.

\* :meth:`on_consume_ready`

Handler called when the consumer is ready to accept messages.

Takes arguments (connection, channel, consumers). Also keyword arguments to consume are forwarded to this handler.

\* :meth:`on_consume_end`

Handler called after the consumers are canceled. Takes arguments (connection, channel).

\* :meth:`on_iteration`

Handler called for every iteration while draining events.

Takes no arguments.

\* :meth:`on_decode_error`

Handler called if a consumer was unable to decode the body of a message.

Takes arguments (message, exc) where message is the original message object.

The default handler will log the error and acknowledge the message, so if you override make sure to call super, or perform these steps yourself.

property channel_errors
connect_max_retries = None

maximum number of retries trying to re-establish the connection, if the connection is lost/unavailable.

property connection_errors
consume(limit=None, timeout=None, safety_interval=1, **kwargs)[source]
extra_context(connection, channel)[source]
get_consumers(Consumer, channel)[source]

Use kombu.common.ignore_errors() instead.

on_connection_error(exc, interval)[source]
on_consume_end(connection, channel)[source]
on_consume_ready(connection, channel, consumers, **kwargs)[source]
on_decode_error(message, exc)[source]
property restart_limit
run(_tokens=1, **kwargs)[source]
should_stop = False

When this is set to true the consumer should stop consuming and return, so that it can be joined if it is the implementation of a thread.

class kombu.mixins.ConsumerProducerMixin[source]

Consumer and Producer mixin.

Version of ConsumerMixin having separate connection for also publishing messages.


class Worker(ConsumerProducerMixin):

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

    def get_consumers(self, Consumer, channel):
        return [Consumer(queues=Queue('foo'),

    def handle_message(self, message):
            {'message': 'hello to you'},
on_consume_end(connection, channel)[source]
property producer
property producer_connection