gpt4 book ai didi

python - 当我尝试使用 pika (python) 向 RabbitMQ 确认消息时发生错误 "unknown delivery tag"

转载 作者:IT老高 更新时间:2023-10-28 20:57:23 24 4
gpt4 key购买 nike

我想在几个线程中处理消息,但在执行此代码时出现错误:

from __future__ import with_statement
import pika
import sys
from pika.adapters.blocking_connection import BlockingConnection
from pika import connection, credentials
import time
import threading
import random
from pika.adapters.select_connection import SelectConnection
from pika.connection import Connection
import traceback


def doWork(body, args, channel):


r = random.random()
time.sleep(r * 10)
try:
channel.basic_ack(delivery_tag=args.delivery_tag)

except :
traceback.print_exc()


auth = credentials.PlainCredentials(username="guest", password="guest")
params = connection.ConnectionParameters(host="localhost", credentials=auth)
conn = BlockingConnection(params)
channel = conn.channel()


while True:

time.sleep(0.03)
try:

method_frame, header_frame, body = channel.basic_get(queue="test_queue")
if method_frame.NAME == 'Basic.GetEmpty':
continue

t = threading.Thread(target=doWork, args=[body, method_frame, channel])
t.setDaemon(True)
t.start()

except Exception, e:
traceback.print_exc()
continue

错误说明:

Traceback (most recent call last):  File "C:\work\projects\mq\start.py", line 43, in     method_frame, header_frame, body = channel.basic_get(queue="test_queue")  File "C:\work\projects\mq\libs\pika\adapters\blocking_connection.py", line 318, in basic_get    self.basic_get_(self, self._on_basic_get, ticket, queue, no_ack)  File "C:\work\projects\mq\libs\pika\channel.py", line 469, in basic_get    no_ack=no_ack))  File "C:\work\projects\mq\libs\pika\adapters\blocking_connection.py", line 244, in send_method    self.connection.process_data_events()  File "C:\work\projects\mq\libs\pika\adapters\blocking_connection.py", line 94, in process_data_events    self._handle_read()  File "C:\work\projects\mq\libs\pika\adapters\base_connection.py", line 162, in _handle_read    self._on_data_available(data)  File "C:\work\projects\mq\libs\pika\connection.py", line 589, in _on_data_available    frame)                 # Args  File "C:\work\projects\mq\libs\pika\callback.py", line 124, in process    callback(*args, **keywords)  File "C:\work\projects\mq\libs\pika\adapters\blocking_connection.py", line 269, in _on_remote_close    frame.method.reply_text)AMQPChannelError: (406, 'PRECONDITION_FAILED - unknown delivery tag 204')

版本:pika 0.9.5、rabbitMQ 2.6.1

最佳答案

问题可能是您像这样设置 no_ack=True:

consumer_tag = channel.basic_consume(
message_delivery_event,
no_ack=True,
queue=queue,
)

然后确认消息:

channel.basic_ack(delivery_tag=args.delivery_tag)

您必须选择是否要确认并设置正确的消费参数。

关于python - 当我尝试使用 pika (python) 向 RabbitMQ 确认消息时发生错误 "unknown delivery tag",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9392478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com