我已经从安装了 3.5.3,我希望订阅某些回调,我可以使用下面的https://pypi.org/project/redis/程序成功地接收这些回调:
import redis
def main():
try:
r = redis.Redis(host='localhost', port=26379, username='myusername', password='mypassword')
p = r.pubsub()
p.psubscribe('+sdown')
p.psubscribe('-sdown')
p.psubscribe('+switch-master')
p.subscribe('+sentinel')
while True:
message = p.get_message()
if message:
print(message)
except Exception as ex:
print(ex) <-- "Connection closed by server"但在整整120秒钟之后,我碰到了错误消息“连接被服务器关闭”的异常。
我怎么才能避免这种情况?在任何配置文件中是否有我可以更改的设置?
或者我能把任何参数传递给Redis吗?
我让redis实例运行如下:
ps -ef | grep redis
redis 1549 1 0 14:29 ? 00:00:06 /usr/sbin/redis-server 127.0.0.1:6379
redis 5209 1 0 15:55 ? 00:00:00 /usr/sbin/redis-sentinel *:26379 [sentinel]发布于 2021-04-08 18:43:44
您将redis指向端口26379,但redis不在该端口上运行--它运行在6379。你确实有哨兵在26379上跑。你有两个选择-
如果您想使用
。
下面的代码来自redis-py文档中的Sentinel support部分
from redis.sentinel import Sentinel
sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
master = sentinel.master_for('mymaster', socket_timeout=0.1)
slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
master.set('foo', 'bar')
slave.get('foo')https://stackoverflow.com/questions/67009310
复制相似问题