use exchange instead of queue to allow multiple subscribers
This commit is contained in:
@@ -1,28 +1,26 @@
|
||||
import pika, sys, os
|
||||
import pika
|
||||
|
||||
# see https://www.rabbitmq.com/tutorials/tutorial-one-python.html
|
||||
# see https://www.rabbitmq.com/tutorials/tutorial-three-python.html
|
||||
|
||||
def main():
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='hello')
|
||||
channel.exchange_declare(exchange='logs', exchange_type='fanout')
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(" [x] Received %r" % body)
|
||||
result = channel.queue_declare(queue='', exclusive=True)
|
||||
queue_name = result.method.queue
|
||||
|
||||
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
|
||||
channel.queue_bind(exchange='logs', queue=queue_name)
|
||||
|
||||
print(' [*] Waiting for messages. To exit press CTRL+C')
|
||||
channel.start_consuming()
|
||||
print(' [*] Waiting for logs. To exit press CTRL+C')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except KeyboardInterrupt:
|
||||
print('Interrupted')
|
||||
try:
|
||||
sys.exit(0)
|
||||
except SystemExit:
|
||||
os._exit(0)
|
||||
def callback(ch, method, properties, body):
|
||||
print(" [x] %r" % body)
|
||||
|
||||
|
||||
channel.basic_consume(
|
||||
queue=queue_name, on_message_callback=callback, auto_ack=True)
|
||||
|
||||
channel.start_consuming()
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import pika
|
||||
import sys
|
||||
|
||||
# see https://www.rabbitmq.com/tutorials/tutorial-one-python.html
|
||||
# see https://www.rabbitmq.com/tutorials/tutorial-three-python.html
|
||||
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='hello')
|
||||
channel.exchange_declare(exchange='logs', exchange_type='fanout')
|
||||
|
||||
channel.basic_publish(exchange='', routing_key='hello', body=b'Hello World!')
|
||||
print(" [x] Sent 'Hello World!'")
|
||||
message = ' '.join(sys.argv[1:]) or b"info: Hello World!"
|
||||
channel.basic_publish(exchange='logs', routing_key='', body=message)
|
||||
print(" [x] Sent %r" % message)
|
||||
connection.close()
|
||||
|
||||
Reference in New Issue
Block a user