皮卡吃得停不下来

问题描述 投票:0回答:2

我无法从 Pika 返回数据,因为它

start_consuming
没有停止。它打印结果但不返回输出

def on_request(ch, method, props, body):
    directory =body

    print(directory.decode('utf-8'))
    response = parse(directory.decode('utf-8'))

    ch.basic_publish(exchange='',
                     routing_key=props.reply_to,
                     properties=pika.BasicProperties(correlation_id = \
                                                         props.correlation_id),
                     body=str(response))
    ch.basic_ack(delivery_tag=method.delivery_tag)
def start():
    print("hi")
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host='localhost'))

    channel = connection.channel()


    channel.queue_declare(queue='rpc_queue')

    channel.basic_qos(prefetch_count=2)
    channel.basic_consume(queue='rpc_queue', on_message_callback=on_request)

    print(" [x] Awaiting RPC requests")
    channel.start_consuming()
python python-3.x pika
2个回答
1
投票

按照设计

start_consuming
永远阻塞。您必须在
on_request
方法中取消消费者。

您还可以使用此方法来消费消息,该消息允许设置

inactivity_timeout
,然后您可以在其中取消您的消费者。

最后,

SelectConnection
在与 Pika 的 I/O 循环交互方面提供了更大的灵活性,当您的需求比
BlockingConnection
支持的更复杂时,建议您使用。


-1
投票

只需使用channel.stop_consuming()

© www.soinside.com 2019 - 2024. All rights reserved.