RabbitMQ pika.exceptions.ConnectionClosed(-1,“error(104,'peer reset by peer')”)

问题描述 投票:1回答:1

我在RabbitMQ中有一个任务队列,其中包含多个生产者(12)和一个用于Web应用程序中繁重任务的消费者。当我运行消费者时,它会在崩溃之前开始出现一些消息:

Traceback (most recent call last):
File "jobs.py", line 42, in <module> jobs[job](config)
File "/home/ec2-user/project/queue.py", line 100, in init_queue
channel.start_consuming()
File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 1822, in start_consuming
self.connection.process_data_events(time_limit=None)
File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 749, in process_data_events
self._flush_output(common_terminator)
File "/usr/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 477, in _flush_output
result.reason_text)
pika.exceptions.ConnectionClosed: (-1, "error(104, 'Connection reset by peer')")

生产者代码是:

message = {'image_url': image_url, 'image_name': image_name, 'notes': notes}

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.queue_declare(queue='tasks_queue')
channel.basic_publish(exchange='', routing_key=queue_name, body=json.dumps(message))

connection.close()

唯一的消费者代码(一个是冲突):

def callback(self, ch, method, properties, body):
    """Callback when receive a message."""
    message = json.loads(body)
    try:
        image = _get_image(message['image_url'])
    except:
        sys.stderr.write('Error getting image in note %s' % note['id'])
   # Crop image with PIL. Not so expensive
   box_path = _crop(image, message['image_name'], box)

   # API call. Long time function
   result = long_api_call(box_path)

   if result is None:
       sys.stderr.write('Error in note %s' % note['id'])
       return
   # update the db
   db.update_record(result)


connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='tasks_queue')
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback_obj.callback, queue='tasks_queue', no_ack=True)
channel.start_consuming()

如您所见,消息有3种昂贵的功能。一个裁剪任务,一个API调用和一个数据库更新。如果没有API调用,que consumer将顺利运行。

提前致谢

python python-2.7 rabbitmq pika python-pika
1个回答
2
投票

您的RabbitMQ日志显示我认为可能会看到的消息:

missed heartbeats from client, timeout: 60s

发生了什么事情,你的long_api_call阻止了Pika的I / O循环。 Pika是一个非常轻量级的库,不会在后台启动线程,因此您必须以不会阻止Pika的I / O循环比心跳间隔更长的方式进行编码。 RabbitMQ认为您的客户已经死亡或没有响应,并强行关闭连接。

请参阅my answer here,它链接到this example code,显示如何在单独的线程中正确执行长时间运行的任务。你仍然可以使用no_ack=True,你将跳过ack_message电话。


注意:RabbitMQ团队监控the rabbitmq-users mailing list,有时只回答StackOverflow上的问题。

© www.soinside.com 2019 - 2024. All rights reserved.