【问题标题】:rabbitmq, python - ack consumer program examplerabbitmq, python - ack 消费者程序示例
【发布时间】:2019-06-07 09:44:34
【问题描述】:

我正在寻找 Python 中的基本 Rabbitmq ack 消费者程序。到目前为止,我有基本的 ack producer 程序。但是不知道对不对。

producer.py

import pika, socket

credentials = pika.PlainCredentials('xxxx', '1234')
hostname = socket.gethostname()
parameters = 
pika.ConnectionParameters(host=socket.gethostbyname(hostname), 
port=5672, virtual_host='/', credentials=credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()

msg_props = pika.BasicProperties()
msg_props.content_type = "text/plain"

channel.queue_declare(queue='hello')
if channel.basic_publish(exchange='', routing_key='hello', body='Hello 
World!', properties=msg_props):
     print ("Message Acknowledged")
else:
     print ("Message Lost")

print("[x] Sent 'Hello World!'")
connection.close()

consumer.py

import pika, socket

credentials = pika.PlainCredentials('xxxx', '1234')
hostname = socket.gethostname()
parameters = 
pika.ConnectionParameters(host=socket.gethostbyname(hostname), 
port=5672, virtual_host='/', credentials=credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()

channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)
    channel.basic_ack(method.delivery_tag)

channel.basic_consume(queue='hello', on_message_callback=callback, 
auto_ack=False)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
>python consumer.py

 [*] Waiting for messages. To exit press CTRL+C
 [x] Received b'Hello World!'


>python producer.py

 Message Lost
 [x] Sent 'Hello World!'

输出:

Though the message is received at the consumer.py program, the delivery 
note at producer.py says, "Message Lost", but the note should be "Message Acknowledged". 

对于rabbitmq,python中正确的消费者和生产者ack程序是什么?

【问题讨论】:

  • 您使用的是哪个 python 版本?它在 python 3.6 中完美运行。
  • python 3.6,你能发布你的消费程序吗?

标签: python rabbitmq


【解决方案1】:

经过几天又几天的研究,为了得到想要的结果,我终于找到了这个,来自链接 - RabbitMQ Manual ACK on c# client

@Evk 回答, “... BasicAcks 是关于发布者确认,而不是来自接收者的确认。因此,您向代理和代理(因此,RabbitMQ 本身)在处理此消息时(例如 - 当它会将其写入磁盘以获取持久消息,或者将其放入队列中)。请注意,这里不涉及接收器 - 它完全在发布者和 RabbitMQ 之间。

现在,当您在接收者处确认消息时 - 再次仅在接收者和 RabbitMQ 之间进行 - 您告诉 rabbit 消息已被处理并且可以安全地删除。这样做是为了处理接收器在处理过程中崩溃的情况 - 然后 rabbit 将能够将此消息传递给下一个接收器(如果有的话)。

请注意,此类架构的全部目的是将发布者和接收者分开 - 它们不应相互依赖。

如果您有一个接收器(可能有很多)并且您希望确保它处理您的消息 - 使用 RPC 模式:发送消息并等待来自该接收器的另一条消息。”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多