【发布时间】:2019-05-27 08:47:53
【问题描述】:
我有一个从流中读取内容的 Python 脚本,当读取一个新字符串时,它会将其内容(一个字符串)推送到 RabbitMQ 队列。
问题是流可能不会在 1、2 或 9 小时左右发送消息,所以我希望 RabbitMQ 连接始终打开。
问题是当我创建连接和通道时:
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=self.host, credentials=self.credentials))
channel = self.connection.channel()
channel.exchange_declare(exchange=self.exchange_name, exchange_type='fanout')
...如果一个小时后有消息到达,我会收到此错误:
File "/usr/local/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/var/opt/rabbitmq-agent.py", line 34, in push_to_queue
raise Exception("Error sending the message to the queue: " + format(e))
Exception: Error sending the message to the queue: Send message to publisher error: Channel allocation requires an open connection: <SelectConnection CLOSED socket=None params=<ConnectionParameters host=x port=xvirtual_host=/ ssl=False>>
我猜是rabbitmq服务器和客户端之间的连接已经关闭。
我怎样才能避免这种情况?我想要一个“请保持连接始终有效”。或许在 Pika 的连接参数中设置了一个超大的心跳?像这样的:
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=self.host, credentials=self.credentials, heartbeat=6000))
任何其他更酷的解决方案都将受到高度赞赏。
提前致谢
【问题讨论】:
标签: python rabbitmq pika rabbitmq-exchange