python - 鼠兔连接丢失 Error : pika. exceptions.StreamLostError : Stream connection lost: ConnectionResetError(104, 'Connection reset by peer' )

标签 python python-3.x rabbitmq mysql-python pika

追溯(最近的调用最后):

文件“download_image_from_queue.py”,第 44 行,在

channel.start_consuming()

文件“/home/justdial/miniconda3/lib/python3.7/site-packages/pika/adapters/blocking_connection.py”,第 1866 行,在 start_consuming 中

self._process_data_events(time_limit=None)

文件“/home/justdial/miniconda3/lib/python3.7/site-packages/pika/adapters/blocking_connection.py”,第 2027 行,在 _process_data_events 中

self.connection.process_data_events(time_limit=time_limit)

文件“/home/justdial/miniconda3/lib/python3.7/site-packages/pika/adapters/blocking_connection.py”,第 825 行,在 process_data_events 中

self._flush_output(common_terminator)

文件“/home/justdial/miniconda3/lib/python3.7/site-packages/pika/adapters/blocking_connection.py”,第 522 行,在 _flush_output 中

raise self._closed_result.value.error

pika.exceptions.StreamLostError: Stream connection lost: ConnectionResetError(104, 'Connection reset by peer'

文件“download_image_from_queue.py”:

def callback(ch, method, properties, body):

    default_flag = True

    try:
        data = json.loads(body.decode("utf-8"))['DATA']

        url = data['url']
        dest_name = data['dest_name']

        default_flag = False
    except:
        print ("\n INCORRECT DATA FORMAT INSERTED INTO THE QUEUE... \n")
        pass

    if default_flag == False:
        os.system("cd /home/images_pred/ && wget -L --timeout=3 --tries=2 {} -O {}".format(url, dest_name))

    print ('\n Waiting for the queue to be filled...       PRESS CTRL+C TO STOP \n')


credentials = pika.PlainCredentials('abcd', 'BCD')
parameters = pika.ConnectionParameters('0.0.0.1', 5672, '/', credentials )


connection = pika.BlockingConnection(parameters)
channel = connection.channel()
#channel.basic_qos(prefetch_count = 1)

channel.basic_consume(queue = 'IMG_DOWNLOAD', auto_ack = False, on_message_callback = callback)
#channel.basic_ack()

print ('\n Waiting for the queue to be filled...       PRESS CTRL+C TO STOP \n')

channel.start_consuming()

一旦开始消费,它就会开始下载,但很快就会出现此错误“pika.exceptions.StreamLostError:流连接丢失:ConnectionResetError(104,'连接由对等方重置')”

谁能帮我解决这个问题?

最佳答案

在作业得到处理并且您的进程返回发送 ACK 之前,有许多心跳丢失,因此连接消失了。

理想方案(推荐)

使用线程并在线程中运行代码。在此处查看示例:

https://github.com/pika/pika/blob/1.0.1/examples/basic_consumer_threaded.py

快速解决方法 [不推荐]

禁用心跳。

如何禁用心跳? --> 创建连接时传递heartbeat=0

pika.ConnectionParameters(
   host=RABBIT_MQ_HOST, credentials=CREDENTIALS, heartbeat=0
)

关于python - 鼠兔连接丢失 Error : pika. exceptions.StreamLostError : Stream connection lost: ConnectionResetError(104, 'Connection reset by peer' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58927722/

相关文章:

python - 没有为第二个单元测试定义全局名称

python - 将 MySQL 结果插入现有的 pandas 数据帧

java - RabbitMQ、docker、单队列、多个消费者

react-native - RabbitMQ React Native 发送示例

python - 无法使用python获取xml文件的元素值

php - RabbitMQ 和 php-amqplib : cunsumer reconnect

安装pywin32后Python 'No module named win32gui'

python - 饼图仅显示标签而不显示数据

python-3.x - VS-Code Python 调试 - ConnectionRefusedError : [WinError 10061] No connection could be made because the target machine actively refused it

python - F查找字符串值的长度,但不查找数字值的长度