kombu/examples/experimental/async_consume.py

31 lines
768 B
Python
Raw Normal View History

2014-02-03 15:40:40 +00:00
#!/usr/bin/env python
from __future__ import absolute_import, unicode_literals
2014-02-03 15:40:40 +00:00
from kombu import Connection, Exchange, Queue, Producer, Consumer
from kombu.async import Hub
hub = Hub()
exchange = Exchange('asynt')
queue = Queue('asynt', exchange, 'asynt')
2014-05-27 13:41:20 +00:00
2014-02-03 15:40:40 +00:00
def send_message(conn):
producer = Producer(conn)
producer.publish('hello world', exchange=exchange, routing_key='asynt')
print('message sent')
2014-02-03 15:40:40 +00:00
def on_message(message):
print('received: {0!r}'.format(message.body))
2014-02-03 15:40:40 +00:00
message.ack()
hub.stop() # <-- exit after one message
if __name__ == '__main__':
conn = Connection('amqp://')
conn.register_with_event_loop(hub)
with Consumer(conn, [queue], on_message=on_message):
send_message(conn)
hub.run_forever()