从 redis pub/sub 中断开的连接中恢复 [英] Recover from dropped connection in redis pub/sub

查看:76
本文介绍了从 redis pub/sub 中断开的连接中恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行连接到 redis 数据库的客户端.客户端使用 WiFi 连接,有时会断开连接.不幸的是,当发生这种情况时,程序只是继续运行而不会抛出任何类型的警告.

I am running client that is connecting to a redis db. The client is on a WiFi connection and will drop the connection at times. Unfortunately, when this happens, the program just keeps running without throwing any type of warning.

r = redis.StrictRedis(host=XX, password=YY...)
ps = r.pubsub()
ps.subscribe("12345")
for items in ps.listen():
    if items['type'] == 'message':
       data = items['data']

理想情况下,我正在寻找的是在连接丢失时捕获一个事件,尝试重新建立连接,进行一些错误纠正,然后让事情恢复正常.这应该在python程序中完成吗?我应该有一个外部看门狗吗?

Ideally, what I am looking for is a catch an event when the connection is lost, try and reestablish the connection, do some error correcting, then get things back up and running. Should this be done in the python program? Should I have an external watchdog?

推荐答案

不幸的是,必须ping"Redis 以检查它是否可用.如果您尝试将值放入 Redis 存储中,如果连接丢失,它将引发 ConnectionError 异常.但是当连接丢失时,listen() 生成器不会自动关闭.

Unfortunately, one have to 'ping' Redis to check if it is available. If You try to put a value to Redis storage, it will raise an ConnectionError exception if connection is lost. But the listen() generator will not close automatically when connection is lost.

我认为破解 Redis 的连接池会有所帮助,试试看吧.

I think that hacking Redis' connection pool could help, give it a try.

附言在不受信任的网络环境中连接redis是非常不安全的.

P.S. In is very insecure to connect to redis in an untrusted network environment.

这篇关于从 redis pub/sub 中断开的连接中恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆