How to wait for messages in multiple queues using py-amqplib

I am using py-amqplib to access RabbitMQ in Python. The app receives listening requests for specific MQ topics from time to time.

The first time he receives such a request, he creates an AMQP connection and channel and starts a new thread to listen to messages:

    connection = amqp.Connection(host = host, userid = "guest", password = "guest", virtual_host = "/", insist = False)
    channel = connection.channel()

    listener = AMQPListener(channel)
    listener.start()

AMQPListener is very simple:

class AMQPListener(threading.Thread):
    def __init__(self, channel):
        threading.Thread.__init__(self)
        self.__channel = channel

    def run(self):
        while True:
            self.__channel.wait()

After creating the connection, it joins the topic of interest, for example:

channel.queue_declare(queue = queueName, exclusive = False)
channel.exchange_declare(exchange = MQ_EXCHANGE_NAME, type = "direct", durable = False, auto_delete = True)
channel.queue_bind(queue = queueName, exchange = MQ_EXCHANGE_NAME, routing_key = destination)

def receive_callback(msg):
    self.queue.put(msg.body)

channel.basic_consume(queue = queueName, no_ack = True, callback = receive_callback)

. . AMQP AMQPListener ( ), channel.queue_declare() . , connection.channel() .

, , - , (.. routing_key), . , wait() - , , . , ( ) ?

: , ? channel.wait() , . , , - , "" , .. .

+3
1

, comsumer , basic_consume() channel.wait() . , basic_consume(). , basic_consume().

channel.basic_cancel (consumer_tag), ( ).

+1

Source: https://habr.com/ru/post/1724068/


All Articles