【发布时间】:2019-03-29 02:53:18
【问题描述】:
我在 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 consumer 运行流畅。
提前致谢
【问题讨论】:
-
请提供有关您的环境的信息 - 您正在使用什么版本的软件,您使用的是 Docker,您使用的是负载均衡器,RabbitMQ 是否记录了任何内容。
Connection reset by peer表示某些东西意外中断了您的 TCP 连接。我希望看到 RabbitMQ 记录的类似消息。 -
你好。我在 Amazon Linux EC2 实例上运行 rabbitmq 3.7.0。没有 docker 或负载均衡器。另外,这段代码。 result = long_api_call(box_path) 在 try catch 块后面,因此应该是容错的。此 long_api_call 指向当前 Internet 连接不稳定的外部服务,因此某些回调调用不起作用并不罕见。但是这个错误不应该因为这个奇怪的错误而让消费者失望。我的 rabbitmq 日志文件:
-
2018-10-25 06:04:54.854 [info] 关闭 AMQP 连接 (127.0.0.1:42882 -> 127.0.0.1:5672, vhost: '/', user: 'guest') 2018-10-25 06:05:14.740 [警告] 关闭 AMQP 连接 (127.0.0.1:32816 -> 127.0.0.1:5672) :错过来自客户端的心跳,超时:60 秒 2018-10-25 06:06:59.367 [信息] 接受 AMQP 连接 (127.0.0.1:43332 -> 127.0.0.1:5672) 2018 -10-25 06:06:59.370 [信息] 连接 (127.0.0.1:43332 -> 127.0.0.1:5672): 用户 'guest' 已通过身份验证和授予
标签: python python-2.7 rabbitmq pika python-pika