File: blocking_consume.rst

package info (click to toggle)
python-pika 0.9.14-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,048 kB
  • ctags: 2,110
  • sloc: python: 10,046; makefile: 134
file content (29 lines) | stat: -rw-r--r-- 1,357 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Using the Blocking Connection to consume messages from RabbitMQ
===============================================================

.. _example_blocking_basic_consume:

The :py:meth:`BlockingChannel.basic_consume <pika.adapters.blocking_connection.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 :py:class:`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 :py:class:`pika.spec.BasicProperties` 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()