Using the Blocking Connection to consume messages from RabbitMQΒΆ

The BlockingChannel.basic_consume() method assign a callback method to be called every time that RabbitMQ delivers messages to your consuming application.

When pika calls your method, it will pass in the channel, a pika.spec.Basic.Deliver() object with the delivery tag, the redelivered flag, the routing key that was used to put the message in the queue, and the exchange the message was published to. The third argument will be a pika.spec.Basic.Properties() object and the last will be the message body.

Example of consuming messages and acknowledging them:

import pika


def on_message(channel, method_frame, header_frame, body):
    print method_frame.delivery_tag
    print body
    print
    channel.basic_ack(delivery_tag=method_frame.delivery_tag)


connection = pika.BlockingConnection()
channel = connection.channel()
channel.basic_consume(on_message, 'test')
try:
    channel.start_consuming()
except KeyboardInterrupt:
    channel.stop_consuming()
connection.close()

Project Versions

Previous topic

Using the Blocking Connection to get a message from RabbitMQ

Next topic

Using the BlockingChannel.consume generator to consume messages

This Page