我使用pika python库连接到localhost上的rabbitmq-server。
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上运行。 RabbitMQ 3.1.3
我不明白发生了什么。你能解释一下吗?
答案 0 :(得分:3)
相关日志行:{heartbeat_timeout,running}。
某些东西阻止了BlockingConnection发送心跳,因此RabbitMQ认为您的客户端无法访问或死机。您有3个选择:
答案 1 :(得分:2)
嗯,实际的问题是我已经停止了rabbitmq-server。而且皮卡不处理断线问题。
答案 2 :(得分:1)
我有一些我想要运行的测试。当我在localhost中运行时,一切都很好。但是,当我在服务器中运行测试时,我遇到了与此问题完全相同的问题和情况。我查看了我的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
,在我的localhost中为2.7.12
。为了测试python版本是否真的是问题,我安装了conda并创建了一个python版本2.7.3
的环境。我跑了测试并且通过了(我不能重现这个问题)。
在完成上述所有尝试后,我想出了另一个假设,即我的服务器rabbitmq-server
中可能存在错误。我比较了这些版本:在localhost中它是最新的(3.6.5
),在我的服务器中它是2.8.4
。为了验证这是实际问题,我运行了测试,但是我使用了带有更高版本的远程rabbitmq。一切都很好。所以,我升级了rabbitmq-server
,而且,看哪,问题就消失了!
<强> TL; DR:强>
解决方案是升级您的rabbitmq-server
!