pika连接RabbitMQ,消费者总报连接关闭

StSphinx 发布于 2017/02/16 09:57
阅读 1K+
收藏 0

在使用pika连接rabbitmq的时候,使用了下面的代码,但是在消费者执行了一段时间后就会报下面的错误,查了些资料和文档,我理解的大概意思是说pika只在consumer在结束连接的时候才会才会给rabbitmq发送心跳信息,但因为一直在处理队列的消息,心跳信息被阻塞了,所以一直未发,导致rabbitmq认为consumer挂了,就吧连接给关了,请教各位大神如何保持consumer一直在监听队列,并保持处理消息的状态。

    #connection
    para = pika.ConnectionParameters('localhost', 5672, '/', heartbeat_interval=0)
    con = pika.BlockingConnection(para)

    channel = con.channel()
    channel.queue.declare('test', durable=True)


    # consumer
    def consumer():
        channel.basic_qos(prefetch_count=1)
        self.channel.basic_consume(callback, queue='test')
        print(' [*] Waiting for messages. To exit press CTRL+C')
        self.channel.start_consuming()

    def callback(ch, method, properties, body):
        print body
        ch.basic_ack(delivery_tag=method.delivery_tag)

Traceback (most recent call last):
  File "TaskWorker.py", line 78, in <module>
    tw.fetch_info_with_soapapi()
  File "TaskWorker.py", line 73, in fetch_info_with_soapapi
    self.channel.start_consuming()
  File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 1681, in start_consuming
    self.connection.process_data_events(time_limit=None)
  File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 656, in process_data_events
    self._dispatch_channel_events()
  File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 469, in _dispatch_channel_events
    impl_channel._get_cookie()._dispatch_events()
  File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 1310, in _dispatch_events
    evt.body)
  File "TaskWorker.py", line 58, in callback
    ch.basic_ack(delivery_tag=method.delivery_tag)
  File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 1889, in basic_ack
    self._flush_output()
  File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 1174, in _flush_output
    *waiters)
  File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 395, in _flush_output
    raise exceptions.ConnectionClosed()
pika.exceptions.ConnectionClosed
加载中
0
Xuanyuan92
Xuanyuan92

你代码中不是把心跳检测关了吗。应该不是心跳问题导致的终止吧

返回顶部
顶部