2014-02-03 15:40:40 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2021-07-20 13:07:49 +00:00
|
|
|
from kombu import Connection, Consumer, Exchange, Producer, Queue
|
2018-03-19 17:28:43 +00:00
|
|
|
from kombu.asynchronous import Hub
|
2014-02-03 15:40:40 +00:00
|
|
|
|
|
|
|
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')
|
2016-04-06 20:14:05 +00:00
|
|
|
print('message sent')
|
2014-02-03 15:40:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def on_message(message):
|
2020-07-13 13:58:06 +00:00
|
|
|
print(f'received: {message.body!r}')
|
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()
|