RabbitMQ pika.exceptions.ConnectionClosed(-1,“错误(104,'对等重置连接')”)

时间:2018-10-24 15:52:16

标签: python python-2.7 rabbitmq pika python-pika

我在RabbitMQ中有一个任务队列,其中有多个生产者(12)和一个使用者,用于处理Webapp中的繁重任务。当我运行使用者时,它会在出现此错误而崩溃之前开始使某些消息出队:

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使用者将运行平稳。

预先感谢

1 个答案:

答案 0 :(得分: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上回答问题。

相关问题