Redis PubSub 没有重新连接

Redis PubSub not reconnecting

我目前正在开发一个围绕 Redis Pubsub 的包装器来获取订阅者实例。我正在努力使其对连接中断具有鲁棒性。 根据文档,“如果发生网络错误或超时等断开连接,PubSub 对象将在重新连接时重新订阅所有先前的通道和模式”。但它似乎对我不起作用。

这是一个 PubSub 流程的工作示例:

>>> import redis
>>> r = redis.Redis(host="172.17.0.2", port=6379)
>>> r.ping()
True
>>> sub = r.pubsub()
>>> sub.subscribe("test")
>>> r.publish("test","test")
1
>>> sub.get_message()
{'type': 'subscribe', 'pattern': None, 'channel': b'test', 'data': 1}
>>> sub.get_message()
{'type': 'message', 'pattern': None, 'channel': b'test', 'data': b'test'}

此时,我关闭了我的 Redis 实例。 r.ping() 结果如预期的那样 redis.exceptions.ConnectionError

此时,我重新启动了我的 Redis 实例。

>>> r.ping()
True
>>> r.publish("test","test")
0
>>> sub.get_message()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/robin/.conda/envs/rb-linkvision/lib/python3.6/site-packages/redis/client.py", line 3617, in get_message
    response = self.parse_response(block=False, timeout=timeout)
  File "/home/robin/.conda/envs/rb-linkvision/lib/python3.6/site-packages/redis/client.py", line 3503, in parse_response
    if not block and not conn.can_read(timeout=timeout):
  File "/home/robin/.conda/envs/rb-linkvision/lib/python3.6/site-packages/redis/connection.py", line 734, in can_read
    return self._parser.can_read(timeout)
  File "/home/robin/.conda/envs/rb-linkvision/lib/python3.6/site-packages/redis/connection.py", line 321, in can_read
    return self._buffer and self._buffer.can_read(timeout)
  File "/home/robin/.conda/envs/rb-linkvision/lib/python3.6/site-packages/redis/connection.py", line 231, in can_read
    raise_on_timeout=False)
  File "/home/robin/.conda/envs/rb-linkvision/lib/python3.6/site-packages/redis/connection.py", line 201, in _read_from_socket
    raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
redis.exceptions.ConnectionError: Connection closed by server.

Redis 连接似乎已正确重新启用,但 PubSub 实例不再工作。我目前正在使用一种解决方法。

>>> sub.reset()
>>> sub.subscribe("test")
>>> sub.get_message()
{'type': 'subscribe', 'pattern': None, 'channel': b'test', 'data': 1}

我对我的解决方法很满意,但文档明确指出这应该自动处理。有什么我做的不正确吗? 感谢您的回答!

使用 health_check_interval 选项创建 redis 客户端就成功了。