ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 一段时间后,RabbitMQ断开了我

2019-07-03 17:45:19  阅读:351  来源: 互联网

标签:python rabbitmq queue amqp pika


我正在尝试不断监听队列,但大约一分钟后(假设我的队列为空)我断开了这个错误:

DEBUG:pika.adapters.blocking_connection:Outbound buffer size: 0
DEBUG:pika.adapters.blocking_connection:Outbound buffer size: 0
ERROR:pika.adapters.base_connection:Read empty data, calling disconnect
DEBUG:pika.adapters.blocking_connection:Handling disconnect
INFO:pika.adapters.blocking_connection:on_connection_closed: None, True
WARNING:pika.adapters.blocking_connection:Received Channel.Close, closing: None
DEBUG:pika.callback:Clearing out '1' from the stack
Traceback (most recent call last):
  File "controller.py", line 59, in <module>
    c.run()
  File "controller.py", line 55, in run
    self.listen_queue() # Blocking function
  File "controller.py", line 25, in listen_queue
    self.channel.start_consuming() # Start consuming
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 814, in start_consuming
    self.connection.process_data_events()
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 168, in process_data_events
    if self._handle_read():
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 272, in _handle_read
    super(BlockingConnection, self)._handle_read()
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 315, in _handle_read
    return self._handle_disconnect()
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 263, in _handle_disconnect
    self._on_connection_closed(None, True)
  File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 321, 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 914, in _on_close
    raise exceptions.ChannelClosed(0, 'Not specified')
pika.exceptions.ChannelClosed: (0, 'Not specified')

这是我的代码:

class RabbitConnector():

    def __init__(self):
        self._connect()

    def _connect(self):
        logger.info('Trying to connect to RabbitMQ')
        while True:
            try:
                conn_broker = pika.BlockingConnection(
                    pika.ConnectionParameters(
                        host=conf.rabbit_server,
                        port=conf.rabbit_port,
                        virtual_host=conf.rabbit_vhost,
                        ssl=conf.rabbit_ssl, # do not set it to True if there is no ssl!
                        heartbeat_interval=conf.rabbit_heartbeat_interval,
                        credentials=pika.PlainCredentials(
                            conf.rabbit_user,
                            conf.rabbit_pass)))
                logger.info('Successfully connected to Rabbit at %s:%s' % (conf.rabbit_server, conf.rabbit_port)) 
                self.channel = conn_broker.channel()
                # Don't dispatch a new message to a worker until it has processed and acknowledged the previous one
                self.channel.basic_qos(prefetch_count=conf.rabbit_prefetch_count)
                status = self.channel.queue_declare(queue=conf.rabbit_queue_name,
                                                    durable=conf.rabbit_queue_durable,
                                                    exclusive=conf.rabbit_queue_exclusive,
                                                    passive=conf.rabbit_queue_passive)
                if status.method.message_count == 0:
                    logger.info("Queue empty")
                else:
                    logger.info('Queue status: %s' % status)                  
                self.channel.queue_bind(
                    queue=conf.rabbit_queue_name,
                    exchange=conf.rabbit_exchange_name,
                    routing_key=conf.rabbit_exchange_routing_key)  
            except (pika.exceptions.AMQPConnectionError, pika.exceptions.AMQPChannelError), e:
                time.sleep(3)
                logger.error('Exception while connecting to Rabbit %s' %e)
            else:
                break

    def get_channel(self):
        return self.channel

解决方法:

当队列空了一段时间时,我遇到了同样的问题.连接丢失了.这是防火墙的问题.检查防火墙规则以获取连接IP

标签:python,rabbitmq,queue,amqp,pika
来源: https://codeday.me/bug/20190703/1368829.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有