Python Redis每2分钟超时一次
我已从安装了Redis 3.5.3 我希望订阅某些回调,我可以使用以下Python程序成功接收这些回调:Python Redis每2分钟超时一次,python,redis,Python,Redis,我已从安装了Redis 3.5.3 我希望订阅某些回调,我可以使用以下Python程序成功接收这些回调: import redis def main(): try: r = redis.Redis(host='localhost', port=26379, username='myusername', password='mypassword') p = r.pubsub() p.psubscribe('+sdown')
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"
您将redis指向端口26379,但redis没有在该端口上运行,而是在6379上运行。26379上确实有哨兵。你有两个选择-
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')
你真的有一个redis的实例在本地主机上运行吗?是的,请参阅上面的编辑。我将尝试切换端口,谢谢。如果我使用localhost,你为什么说我违背了目的?为什么我会得到看起来像是成功的+sdown回调(例如,当我使一个节点脱机时)?我更改了端口号,没有超时问题,但我也没有收到任何+sdown回调(例如)。有什么想法吗?Redis Sentinel是一款管理Redis实例集群的应用程序,如果你只有一个在本地运行,它对你没有任何帮助。您可能没有收到消息的原因有两个-1。有什么东西在发信息吗?从另一个过程?如果没有-没有消息。2.您的
psubscribe
模式-我不知道您在尝试什么,但我不相信“+”是启动正则表达式模式的有效字符。
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')