即使队列存在,通过设置passive=“True”进行查询时,来自pika的“queue_declare”返回“None”

2024-04-20 05:05:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个消费者使用pika,该消费者使用来自say queue“email”的数据

当通过设置passive="True"进行查询时,pika中的queue_declare返回{},即使队列存在。在

我使用web界面创建队列“email”,并且我可以看到它的存在(应该由第三方创建它;我这样做只是为了测试)。在

现在在我的程序中,当我打开频道并开始使用电子邮件队列之前,我想确保队列已经存在,所以我将passive设置为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)

我得到的结果是“None”,而我期望得到一个“ok”,因为队列已经存在。有什么建议吗???是因为在使用web UI声明队列时没有指定回调吗?我只想知道queue是否存在,但是pika queue_declare函数调用一个回调函数作为参数,并在没有给定回调函数时发出抱怨。在


Tags: webtruemessage队列queueemaildefchannel
2条回答

您应该使用回调:

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)

请检查文档中的示例:http://pika.readthedocs.org/en/latest/examples/asynchronous_consumer_example.html

相关问题 更多 >