Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Redis每2分钟超时一次_Python_Redis - Fatal编程技术网

Python Redis每2分钟超时一次

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')

我已从安装了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')
        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上确实有哨兵。你有两个选择-

  • 不要使用哨兵。如果您使用的是localhost,那么您已经无法达到目的;)然后,修复程序只是更改端口号

  • 如果你想使用它,你必须告诉redis py这样做,它不是自动的

  • 以下代码来自redis py文档中的“Sentinel支持”部分

    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')