当前位置: 首页 > 知识库问答 >
问题:

在python中使用rabbitmq时出现“pika.exceptions.ConnectionClosed”错误

刘琨
2023-03-14
import pika
import time


connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)
print ' [*] Waiting for messages. To exit press CTRL+C'

def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,)
    time.sleep( body.count('.') )
    print " [x] Done"
    ch.basic_ack(delivery_tag = method.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,
                      queue='task_queue')

channel.start_consuming()
Traceback (most recent call last):
  File "hullworker2.py", line 242, in <module>
    channel.basic_consume(callback,queue='test_queue2')
  File "/usr/local/lib/python2.7/dist-packages/pika/channel.py", line 211, in basic_consume
    {'consumer_tag': consumer_tag})])
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 904, in _rpc
    self.connection.process_data_events()
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 88, in process_data_events
    if self._handle_read():
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 184, in _handle_read
    super(BlockingConnection, self)._handle_read()
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 300, in _handle_read
    return self._handle_error(error)
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 264, in _handle_error
    self._handle_disconnect()
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 181, in _handle_disconnect
    self._on_connection_closed(None, True)
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 232, in _on_connection_closed
    self._channels[channel]._on_close(method_frame)
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 817, in _on_close
    self._send_method(spec.Channel.CloseOk(), None, False)
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 920, in _send_method
    self.connection.send_method(self.channel_number, method_frame, content)
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 120, in send_method
    self._send_method(channel_number, method_frame, content)
  File "/usr/local/lib/python2.7/dist-packages/pika/connection.py", line 1331, in _send_method
    self._send_frame(frame.Method(channel_number, method_frame))
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 245, in _send_frame
    super(BlockingConnection, self)._send_frame(frame_value)
  File "/usr/local/lib/python2.7/dist-packages/pika/connection.py", line 1312, in _send_frame
    raise exceptions.ConnectionClosed
pika.exceptions.ConnectionClosed

共有1个答案

松兴邦
2023-03-14

有一个heartbeats的概念。它基本上是服务器可以确保客户端仍然连接的一种方式。

当你这么做的时候

time.sleep( body.count('.') )

阻止代码的时间n秒数。这意味着,如果服务器希望发送heartbeat帧来检查您的客户端是否还活着,那么它将无法返回响应,因为您的代码被阻塞,并且不知道heartbeat是否到达。

 sleep(duration)[source]

    A safer way to sleep than calling time.sleep() directly which will keep the adapter from ignoring frames sent from RabbitMQ. The connection will “sleep” or block the number of seconds specified in duration in small intervals.
 类似资料:
  • 问题内容: 我正在尝试获取特定目录中的文件列表,并计算目录中的文件数。我总是收到以下错误: 我的代码是: 我按照此处给出的代码示例进行操作。 我在Pyscripter上运行Python脚本,并且目录/ client_side /确实存在。我的python代码位于根文件夹中,并且有一个名为“ client_side”的子文件夹。有人可以帮我吗? 问题答案: 我决定将代码更改为: 并使用以下调用代码:

  • 我尝试执行下面的python代码 最后一个语句通过以下连接错误。

  • 问题内容: 为什么在Python 3中打印字符串时会收到语法错误? 问题答案: 此错误消息表示你尝试使用Python 3遵循示例或运行使用Python 2t语句的程序: 上面的语句在Python 3中不起作用。在Python 3中,你需要在要打印的值周围添加括号: “ SyntaxError:对’print’的调用中缺少括号”是Python 3.4.2中添加的新错误消息,主要用于帮助试图在运行Py

  • 我对java中的继承不熟悉,我有以下问题。我的基类是Plane,它的子类是PlaneComponent,PlaneComponent的子类是PasengerCom的。我的程序由11个类组成,当我忽略PasengerCom的类时,一切都是正确的。但是当我运行整个程序时,我收到了这个消息:

  • 问题内容: 嗨,我正在关注angularjs的一些教程 我正在使用1.3.0版本的angularjs 这是我的代码 我想我做的和本教程一样。但是当我检查萤火虫时,它给我以下错误。 如何摆脱这个? 问题答案: 在angular1.3.0中,您必须执行以下操作,因为在1.3.0-beta中禁用了Global控制器。参考 它还说您可以通过使用以下代码来获得较早的行为, 但不建议这样做

  • 我正在尝试将OpenGL与Qt一起使用。我正在遵循这个示例:http://qt-project.org/doc/qt-5.0/qtgui/openglwindow.html使用Qt 5 build for msvc2012。最后我得到了以下错误: 错误LNK2019:未解析的外部符号_imp_glClear@4在函数“public:virtual void _uthiscall OpenGLWin