Python and RabbitMQ - Best way to listen to consume events from multiple channels?

后端 未结 2 1796
面向向阳花
面向向阳花 2020-12-24 09:08

I have two, separate RabbitMQ instances. I\'m trying to find the best way to listen to events from both.

For example, I can consume events on one with the following

2条回答
  •  无人及你
    2020-12-24 09:19

    Below is an example of how I use one rabbitmq instance to listen to 2 queues at the same time:

    import pika
    import threading
    
    threads=[]
    def client_info(channel):    
       channel.queue_declare(queue='proxy-python')
       print (' [*] Waiting for client messages. To exit press CTRL+C')
    
    
       def callback(ch, method, properties, body):
           print (" Received %s" % (body))
    
       channel.basic_consume(callback, queue='proxy-python', no_ack=True)
       channel.start_consuming()
    
    def scenario_info(channel):    
       channel.queue_declare(queue='savi-virnet-python')
       print (' [*] Waiting for scenrio messages. To exit press CTRL+C')
    
    
       def callback(ch, method, properties, body):
          print (" Received %s" % (body))
    
       channel.basic_consume(callback, queue='savi-virnet-python', no_ack=True)
       channel.start_consuming()
    
    def manager():
       connection1= pika.BlockingConnection(pika.ConnectionParameters
      (host='localhost'))
       channel1 = connection1.channel()
      connection2= pika.BlockingConnection(pika.ConnectionParameters
      (host='localhost'))
       channel2 = connection2.channel()
       t1 = threading.Thread(target=client_info, args=(channel1,))
       t1.daemon = True
       threads.append(t1)
       t1.start()  
    
       t2 = threading.Thread(target=scenario_info, args=(channel2,))
       t2.daemon = True
       threads.append(t2)
    
    
       t2.start()
       for t in threads:
         t.join()
    
    
     manager()
    

提交回复
热议问题