连接在rabbitmq中由于某些原因在某些时间后关闭

2024-05-19 03:41:59 发布

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

我正在使用pika python库连接到本地主机上的rabbitmq服务器。

class BaseRabbitSender(MessageSender):
    __metaclass__ = ABCMeta

    def __init__(self, host):
        self.node = BaseMessagingNode(host)
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(
            host=host))
        self.channel = self.connection.channel()

    @abstractmethod
    def send_message(self, message):
        pass

    def close_connection(self):
        self.connection.close()


class DirectRabbitSender(BaseRabbitSender):
    def __init__(self, host, queue_name):
        super(DirectRabbitSender, self).__init__(host)
        self.queue_name = queue_name
        self.channel.queue_declare(queue=queue_name, durable=True)

    def send_message(self, message):
        self.channel.basic_publish(exchange='',
                                   routing_key=self.queue_name,
                                   body=message,
                                   properties=pika.BasicProperties(
                                       delivery_mode=2,
                                   ))

    def close_connection(self):
        self.connection.close()

因为某些原因,经过相当长的时间(比如几天),我得到了错误。

 File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 560, in basic_publish
    (properties, body), False)
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 1147, in _send_method
    self.connection.send_method(self.channel_number, method_frame, content)
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 267, in send_method
    self._send_method(channel_number, method_frame, content)
  File "build/bdist.linux-x86_64/egg/pika/connection.py", line 1504, in _send_method
    self._send_frame(frame.Header(channel_number, length, content[0]))
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 410, in _send_frame
    self.process_data_events()
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 236, in process_data_events
    raise exceptions.ConnectionClosed()
ConnectionClosed

Rabbitmq服务器日志

=INFO REPORT==== 3-Mar-2014::15:11:03 ===
accepting AMQP connection <0.26625.0> (127.0.0.1:41846 -> 127.0.0.1:5672)

=ERROR REPORT==== 3-Mar-2014::15:38:12 ===
closing AMQP connection <0.326.0> (127.0.0.1:58580 -> 127.0.0.1:5672):
{heartbeat_timeout,running}

=WARNING REPORT==== 3-Mar-2014::16:11:04 ===
closing AMQP connection <0.26625.0> (127.0.0.1:41846 -> 127.0.0.1:5672):
connection_closed_abruptly

=INFO REPORT==== 3-Mar-2014::16:11:05 ===
accepting AMQP connection <0.27016.0> (127.0.0.1:37776 -> 127.0.0.1:5672)

=ERROR REPORT==== 3-Mar-2014::17:41:05 ===
closing AMQP connection <0.27016.0> (127.0.0.1:37776 -> 127.0.0.1:5672):
{heartbeat_timeout,running}

它在ubuntu 13.10上运行。拉比特MQ 3.1.3

我不明白发生了什么。你能解释一下吗?


Tags: buildselfsendhostqueueegglinuxdef
3条回答

实际上,问题是我已经停止了rabbitmq服务器。而皮卡不处理断开。

相关日志行:{心跳超时,正在运行}。

有些东西阻止了阻塞连接发送心跳信号,因此RabbitMQ认为您的客户端无法访问或已死亡。你有三个选择:

  • 避免阻塞行为
  • 增加心跳间隔
  • 尝试其他连接实现,例如Tornado。

我想做一些测试。当我在本地主机上运行它时,一切都很好。但是,当我在服务器上运行测试时,我遇到了与此完全相同的问题和情况。我检查了我的pika版本,它是0.9.13。顺便说一下,我也在用BlockingConnection

首先,我试着定期给process_data_events()打电话。它不起作用。
其次,每当我发布消息或打开新队列时,我都试图打开一个新连接和通道。它不起作用。
第三,我试图将pika升级到0.9.14。它不起作用。

有人在这里说:https://github.com/pika/pika/issues/397提到了一个socket错误的可能性。所以,我检查了python版本,假设可能有一个bug,并在python的稍后版本中修复了它。在服务器中,python版本是2.7.3,在本地主机中是2.7.12。为了测试python版本是否真的是问题所在,我安装了conda,并使用python版本2.7.3创建了一个环境。我运行了测试,结果通过了(我不能重现这个问题)。

经过以上所有的尝试,我提出了另一个假设,可能是我的服务器rabbitmq-server中的一个bug。我比较了版本:在localhost中是最新的(3.6.5),在我的服务器中是2.8.4。为了验证这是否是实际问题,我运行了测试,但在更高版本中使用了远程rabbitmq。一切正常。所以,我升级了rabbitmq-server,瞧,问题消失了!

TL;DR:
解决方案是升级您的rabbitmq-server

相关问题 更多 >

    热门问题