【问题标题】:python pika rabbitmq exeception what is cause of exception [closed]python pika rabbitmq 异常是什么原因导致异常[关闭]
【发布时间】:2020-06-30 06:54:41
【问题描述】:
import pika,traceback
import xyz
import time
from datetime import datetime
import ast
import error_mail

credentials = pika.PlainCredentials('xxxxx', 'xxxxxx')
parameters = pika.ConnectionParameters('xxxxxxxxx', 5672,         '/',credentials,blocked_connection_timeout=400,heartbeat=600)
connection=pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue='xxxxxxxx')
def callback(ch, method, properties,body):
  try:
    print("*****data******")
    print ("TIMESTAMP[#] {}".format(datetime.now()))
    print(body)
    xyz.yyy(body)
    ch.basic_ack(delivery_tag=method.delivery_tag)
  except Exception as e:
    error = traceback.format_exc()
    print(error)
    error_mail.sent_Mail(error)
channel.basic_consume(
queue='xxxxxxxx', on_message_callback=callback)
print('Waiting for messages')
channel.start_consuming()

ch.basic_ack(delivery_tag=method.delivery_tag) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py”,第 2103 行,在 basic_ack self._impl.basic_ack(delivery_tag=delivery_tag,multiple=multiple) 文件“/home/ubuntu/.local/lib/python2.7/site-packages/pika/channel.py”,第 202 行,在 basic_ack self._raise_if_not_open() _raise_if_not_open 中的文件“/home/ubuntu/.local/lib/python2.7/site-packages/pika/channel.py”,第 1389 行 raise exceptions.ChannelWrongStateError('Channel is closed.') ChannelWrongStateError:通道已关闭。

【问题讨论】:

  • 您好 Sharad,请提供您的代码。这样我们就可以为您提供帮助
  • 嗨 Guillem,我在问题中更新了我的代码
  • 提供一些上下文或解释要求将有助于我们更好地理解问题。
  • 同时提供发布者代码也不错

标签: python rabbitmq pika


【解决方案1】:

从这里:RabbitMQ pika.exceptions.ConnectionClosed

这是因为你让主线程等待,因为这个 pika 无法处理传入的消息;在这种情况下,它在子进程完成之前无法响应心跳。这会导致 RabbitMQ 认为客户端已死并强制断开连接。 如果您希望它与心跳一起使用(推荐),您需要定期调用 connection.process_data_events。这可以通过添加一个循环来检查线程是否完成,并且每 30 秒左右调用一次 process_data_events 直到线程完成。

在 hello world 示例中尝试不使用心跳:https://www.rabbitmq.com/tutorials/tutorial-one-python.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 2013-01-06
    相关资源
    最近更新 更多