RabbitMQ pika.exceptions.ConnectionClosed和ChannelClosed心跳

时间:2018-05-18 13:27:13

标签: python rabbitmq pika

我偶尔会出现一个RabbitMQ pika.exceptions.ConnectionClosed异常,我已经查看了各种来源,包括

StackOverflow post

它说使用心跳设置为0并在循环中使用connection.process_data_events,我在代码中使用但仍然得到异常。

以下是例外:

  

文件“/home/developer5/testfiles/dreamlead/text_cleaner/rpc_client.py”,第50行,正在通话中       体= STR(本体))     在basic_publish中输入文件“/usr/local/lib/python3.5/dist-packages/pika/adapters/blocking_connection.py”,第2077行       强制性,立即)     发布文件“/usr/local/lib/python3.5/dist-packages/pika/adapters/blocking_connection.py”,第2164行       self._flush_output()     在_flush_output中输入文件“/usr/local/lib/python3.5/dist-packages/pika/adapters/blocking_connection.py”,第1250行       *服务员)     在_flush_output中输入文件“/usr/local/lib/python3.5/dist-packages/pika/adapters/blocking_connection.py”,第474行       result.reason_text)   pika.exceptions.ConnectionClosed:( - 1,“ConnectionResetError(104,'由peer重置连接')”)

在处理上述异常期间,发生了另一个异常:

  

追踪(最近一次通话):     在handle_delivery中输入“app.py”,第120行       registry_text = text_rpc_client.call(rpc_request_body)     文件“/home/developer5/testfiles/dreamlead/text_cleaner/rpc_client.py”,第67行,正在通话中       self.channel.close()     文件“/usr/local/lib/python3.5/dist-packages/pika/adapters/blocking_connection.py”,第1426行,关闭       self._cancel_all_consumers()     在_cancel_all_consumers中输入“/usr/local/lib/python3.5/dist-packages/pika/adapters/blocking_connection.py”,第1389行       self.basic_cancel(consumer_tag)     在basic_cancel中输入文件“/usr/local/lib/python3.5/dist-packages/pika/adapters/blocking_connection.py”,第1675行       consumer_tag中的consumer_tag,另一个是consumer_tag   断言错误:ctag1.c0482e50664c4582be6376dd0fd1eafb

以下是RabbitMqLogs:

  

关闭AMQP连接< 0.12978.0> (127.0.0.1:47224 - > 127.0.0.1:5672,   客户端意外关闭了TCP连接   错过来自客户的心跳,超时:60秒   = ERROR REPORT ==== 18-May-2018 :: 17:01:40 ===   关闭AMQP连接< 0.12942.0> (127.0.0.1:47214 - > 127.0.0.1:5672):   错过来自客户的心跳,超时:60秒   = ERROR REPORT ==== 18-May-2018 :: 17:09:12 ===   关闭AMQP连接< 0.12602.0> (127.0.0.1:46896 - > 127.0.0.1:5672):

以下是代码:

import pika
import uuid
from socket import error as SocketError
import errno
import traceback
from logging_settings import app_log

class RpcClient(object):
    def __init__(self, conf):
        credentials = pika.PlainCredentials(conf.uname, conf.password)
        parameters = pika.ConnectionParameters(host=conf.host, 
        credentials=credentials, connection_attempts=1000,
        retry_delay=5,heartbeat_interval=25, socket_timeout=30000000)
        self.connection = pika.BlockingConnection(parameters)
        self.rpc_exchange = conf.rpc_exchange
        self.rpc_key = conf.rpc_key
        self.rpc_header_key = conf.rpc_header_key
        self.rpc_header_value = conf.rpc_header_value

        print('credentials: ',credentials)
        print('Connection: ', self.connection)
        print('rpc_key: ', self.rpc_header_key)

        self.channel = self.connection.channel()
        print('Channel: ', self.channel)

        result = self.channel.queue_declare(exclusive=True)
        self.callback_queue = result.method.queue

        self.channel.basic_consume(self.on_response, no_ack=True,
                                   queue=self.callback_queue)
        print('----------------')

    def on_response(self, ch, method, props, body):
        if self.corr_id == props.correlation_id:
            self.response = str(body, 'utf-8')

    def call(self, body):
        try:
            self.response = None
            self.corr_id = str(uuid.uuid4())
            print('Call Body: ', body)
            self.channel.basic_publish(exchange=self.rpc_exchange,
                                       routing_key=self.rpc_key,
                                       properties=pika.BasicProperties(
                                           reply_to=self.callback_queue,
                                           correlation_id=self.corr_id,
                                           headers={self.rpc_header_key: self.rpc_header_value}
                                       ),
                                       body=str(body))
            while self.response is None:
                self.connection.process_data_events()
            return self.response
        # except pika.exceptions.ConnectionClosed as err:
        #     print('----------Connection Closed------------')
        #     app_log.error("Connection closed error: " + traceback.format_exc())
        #     time.sleep(1)
        #     continue
        #     # raise err
        # except pika.exceptions.ChannelClosed as err:
        #     print('----------Channel Closed------------')
        #     app_log.error("Channel Closed error: " + traceback.format_exc())
        #     time.sleep(1)
        #     continue
        except:
            if self.channel is not None:
                self.channel.close()
            if self.connection is not None:
                self.connection.close()

也许问题与我的电脑有关? “ConnectionResetError(104,'由peer重置连接')”)?

或在我的代码中放置 self.connection.process_data_events()

0 个答案:

没有答案