2010-11-24 11:31:20 +00:00
|
|
|
"""
|
|
|
|
Example of simple consumer that waits for a single message, acknowledges it
|
|
|
|
and exits.
|
2016-04-06 20:14:05 +00:00
|
|
|
|
2010-11-24 11:31:20 +00:00
|
|
|
"""
|
2016-04-06 20:14:05 +00:00
|
|
|
|
2010-11-24 11:31:20 +00:00
|
|
|
from pprint import pformat
|
|
|
|
|
2016-04-06 20:14:05 +00:00
|
|
|
from kombu import Connection, Exchange, Queue, Consumer, eventloop
|
|
|
|
|
2010-11-24 11:31:20 +00:00
|
|
|
#: By default messages sent to exchanges are persistent (delivery_mode=2),
|
|
|
|
#: and queues and exchanges are durable.
|
2013-12-11 12:50:40 +00:00
|
|
|
exchange = Exchange('kombu_demo', type='direct')
|
|
|
|
queue = Queue('kombu_demo', exchange, routing_key='kombu_demo')
|
2010-11-24 11:31:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pretty(obj):
|
|
|
|
return pformat(obj, indent=4)
|
|
|
|
|
2010-11-30 18:58:04 +00:00
|
|
|
|
2010-11-24 11:31:20 +00:00
|
|
|
#: This is the callback applied when a message is received.
|
|
|
|
def handle_message(body, message):
|
2020-07-13 13:58:06 +00:00
|
|
|
print(f'Received message: {body!r}')
|
|
|
|
print(' properties:\n{}'.format(pretty(message.properties)))
|
|
|
|
print(' delivery_info:\n{}'.format(pretty(message.delivery_info)))
|
2010-11-24 11:31:20 +00:00
|
|
|
message.ack()
|
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
#: Create a connection and a channel.
|
|
|
|
#: If hostname, userid, password and virtual_host is not specified
|
|
|
|
#: the values below are the default, but listed here so it can
|
|
|
|
#: be easily changed.
|
2013-12-11 12:50:40 +00:00
|
|
|
with Connection('amqp://guest:guest@localhost:5672//') as connection:
|
2012-09-21 13:18:16 +00:00
|
|
|
|
2011-09-07 14:21:38 +00:00
|
|
|
#: Create consumer using our callback and queue.
|
|
|
|
#: Second argument can also be a list to consume from
|
|
|
|
#: any number of queues.
|
2012-06-24 15:32:17 +00:00
|
|
|
with Consumer(connection, queue, callbacks=[handle_message]):
|
|
|
|
|
2013-09-10 16:26:12 +00:00
|
|
|
#: Each iteration waits for a single event. Note that this
|
|
|
|
#: event may not be a message, or a message that is to be
|
|
|
|
#: delivered to the consumers channel, but any event received
|
|
|
|
#: on the connection.
|
|
|
|
for _ in eventloop(connection):
|
|
|
|
pass
|