Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"queue_declare" from pika returns 'None" when queried by setting passive="True" even when queue exists

I am using pika for a consumer that consumes data from say queue "email"

queue_declare from pika returns None when queried by setting passive="True" even when queue exists.

I create the queue "email" using the web interface, and I can see that it exists (a third party is supposed to create it; I am only doing this for testing).

Now in my program when I open the channel and before I start consuming on the email queue, I want to make sure that the queue already exists, so I set passive to be True:

def message(channel, envelope, properties, body):
    if send(envelope.routing_key, body):
        channel.basic_ack(envelope.delivery_tag)
        return
    print("Could not send message.")

def channel_open(channel):
    QUEUES = CONFIG._defaults['queues']
    queuelist = QUEUES.split(",")
    for queuename in queuelist:
        result = channel.queue_declare(message,queue=queuename, passive=True)
        if not result:
            raise NameError("declare the queues specified "
                            "in default config section first")
        channel.basic_consume(queue=queuename, consumer_callback=message)

What I get in result is "None" while I was expecting to get an "ok" since queue already exists. Any pointers??? Is it because when the queue is declared using web UI the callback is not specified? I just want to know if queue exists but pika queue_declare function calls for a callback function as argument and complains when one is not given.

like image 202
user2574872 Avatar asked Feb 22 '26 23:02

user2574872


1 Answers

You should use callbacks:

def qdeclare_callback(method_frame):
    if not method_frame: # method_frame is a result from queue_declare:
        raise NameError("declare the queues specified "
                        "in default config section first")
    # channel.basic_consume(queue=queuename, consumer_callback=message)
    channel.basic_consume(queue=method_frame.method.queue,
                          consumer_callback=message)

# ...
result = channel.queue_declare(qdeclare_callback, queue=queuename, passive=True)

check this example from docs: http://pika.readthedocs.org/en/latest/examples/asynchronous_consumer_example.html

like image 165
ndpu Avatar answered Feb 24 '26 13:02

ndpu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!