首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python每2分钟超时一次

Python每2分钟超时一次
EN

Stack Overflow用户
提问于 2021-04-08 17:48:34
回答 1查看 350关注 0票数 0

我已经从安装了 3.5.3,我希望订阅某些回调,我可以使用下面的https://pypi.org/project/redis/程序成功地接收这些回调:

代码语言:javascript
复制
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实例运行如下:

代码语言:javascript
复制
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]
EN

回答 1

Stack Overflow用户

发布于 2021-04-08 18:43:44

您将redis指向端口26379,但redis不在该端口上运行--它运行在6379。你确实有哨兵在26379上跑。你有两个选择-

  1. 不使用哨兵。如果您正在使用localhost,那么无论如何您已经在破坏这个目的;)修复方法只是更改端口号。

如果您想使用

  1. ,您必须告诉redis-py这样做,这不是自动的。

下面的代码来自redis-py文档中的Sentinel support部分

代码语言:javascript
复制
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')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67009310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档