Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “如何检测”;连接打开时插座关闭;皮卡舞中的错误?_Python_Python 3.x_Pika - Fatal编程技术网

Python “如何检测”;连接打开时插座关闭;皮卡舞中的错误?

Python “如何检测”;连接打开时插座关闭;皮卡舞中的错误?,python,python-3.x,pika,Python,Python 3.x,Pika,我正在使用pika接收来自rabbitmq的消息。如果一个连接长时间未使用,当我再次开始使用它时,我在日志中发现“连接打开时套接字关闭”错误,但不会抛出任何错误。您也无法通过检查“BlockingConnection.is_close”属性来检测此问题 我写了一些代码,试图在“is_close”为真时重新稳定连接,但没有成功。然后,我的脚本将在channel.consume()方法中被阻止,因为无法检测到上述问题 while True: try:

我正在使用pika接收来自rabbitmq的消息。如果一个连接长时间未使用,当我再次开始使用它时,我在日志中发现“连接打开时套接字关闭”错误,但不会抛出任何错误。您也无法通过检查“BlockingConnection.is_close”属性来检测此问题

我写了一些代码,试图在“is_close”为真时重新稳定连接,但没有成功。然后,我的脚本将在channel.consume()方法中被阻止,因为无法检测到上述问题

 while True:
            try:
                if mainStatus.status == SERVING: # my program status. In this case it should start serving.
                    if connection.is_closed:
                        connection.close()
                        connection = pika.BlockingConnection(parameters)
                        ch = connection.channel()
                        ch.queue_declare(queue=queue_name, durable=True)

                    for message in ch.consume(queue_name,inactivity_timeout=1):
                        if message:
                            method, properties, body=message
                        else:
                            method, properties, body=None,None,None
                        if mainStatus.status!=SERVING:
                            if method: mainStatus.ch.basic_nack(method.delivery_tag)
                            break
                        elif method is not None:# Serving and receive a message
                           serve_a_message(mainStatus.ch,db,method,properties,body)
                        else:# normal timeout
                            continue
......

我希望通过检查“is_close”或捕获异常来检测“Socket closed”错误,但除了日志中的错误消息外,什么都没有发生。

您的AMQP代理记录了什么?如果您使用的是RabbitMQ,我猜它会因为错过心跳而关闭连接。如果您的代码阻止Pika的I/O循环,它将无法发送心跳消息。您的AMQP代理记录了什么?如果您使用的是RabbitMQ,我猜它会因为错过心跳而关闭连接。如果您的代码阻塞了Pika的I/O循环,它将无法发送心跳消息。