redis:达到的最大客户端数

2024-09-26 18:20:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个redis缓存,每天设置100次。在完美运行了几天后,我得到了连接错误“已达到最大客户端数”。重新启动服务器后,它现在可以正常工作,但是我希望以后避免这个问题。

在我看来,一旦我创建了一个客户机对象,它就停留在连接池中,不会被终止或删除。

这是我的密码

r = redis.StrictRedis(host= host, port=6379, db=0)
r.set(key_name, data)

这是在一个迭代中。而且,我在python中使用redis。


Tags: 对象keyname服务器redishost密码客户端
2条回答

如果不关闭客户端连接,它将永远保持打开状态。

https://redis.io/topics/clients

Client timeouts By default recent versions of Redis don't close the connection with the client if the client is idle for many seconds: the connection will remain open forever.

However if you don't like this behavior, you can configure a timeout, so that if the client is idle for more than the specified number of seconds, the client connection will be closed.

You can configure this limit via redis.conf or simply using CONFIG SET timeout .

我认为你的redis连接在每次请求时都在实例化,导致它达到最大连接限制,你应该将你的redis实例保持在全局中,这样会共享同一个redis实例,这样就不会再导致太多的连接。redis实例将有自己的连接池,可以通过将max_connections参数设置为redis.connection pool来限制连接数。如果设置了max_connections,则当达到池的限制时,此对象将引发redis.ConnectionError。

POOL = redis.ConnectionPool(host= host, port=6379, db=0)
r = redis.StrictRedis(connection_pool=POOL)

相关问题 更多 >

    热门问题