如何在 redis 中正确使用连接池? [英] How do I properly use connection pools in redis?

查看:82
本文介绍了如何在 redis 中正确使用连接池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不清楚连接池是如何工作的,以及如何正确使用它们.我希望有人可以详细说明.我在下面勾勒出我的用例:

It's not clear to me how connections pools work, and how to properly use them. I was hoping someone could elaborate. I've sketched out my use case below:

settings.py:

settings.py:

import redis

def get_redis_connection():
    return redis.StrictRedis(host='localhost', port=6379, db=0)

task1.py

import settings

connection = settings.get_redis_connection()

def do_something1():
    return connection.hgetall(...)

task2.py

import settings

connection = settings.get_redis_connection()

def do_something1():
    return connection.hgetall(...)

基本上我有一个返回redis连接的setting.py文件,以及几个获取redis连接然后运行操作的不同任务文件.所以每个任务文件都有自己的 redis 实例(这大概是非常昂贵的).优化此过程的最佳方法是什么.是否可以在此示例中使用连接池?有没有更有效的方法来设置这种模式?

Basically I have a setting.py file that returns redis connections, and several different task files that get the redis connections, and then run operations. So each task file has its own redis instance (which presumably is very expensive). What's the best way of optimizing this process. Is it possible to use connection pools for this example? Is there a more efficient way of setting up this pattern?

对于我们的系统,我们有十多个任务文件遵循相同的模式,我注意到我们的请求速度变慢了.

For our system, we have over a dozen task files following this same pattern, and I've noticed our requests slowing down.

谢谢

推荐答案

Redis-py 为您提供了一个连接池,您可以从中检索连接.连接池创建一组您可以根据需要使用的连接(完成后 - 连接返回到连接池以供进一步重用).尝试在不丢弃连接的情况下动态创建连接(即不使用池或不正确使用池)将使您有太多连接到 redis(直到达到连接限制).

Redis-py provides a connection pool for you from which you can retrieve a connection. Connection pools create a set of connections which you can use as needed (and when done - the connection is returned to the connection pool for further reuse). Trying to create connections on the fly without discarding them (i.e. not using a pool or not using the pool correctly) will leave you with way too many connections to redis (until you hit the connection limit).

您可以选择在 init 方法中设置连接池并使池全局化(如果对全局不满意,您可以查看其他选项).

You could choose to setup the connection pool in the init method and make the pool global (you can look at other options if uncomfortable with global).

redis_pool = None

def init():
    global redis_pool
    print("PID %d: initializing redis pool..." % os.getpid())
    redis_pool = redis.ConnectionPool(host='10.0.0.1', port=6379, db=0)

然后您可以像这样从池中检索连接:

You can then retrieve the connection from a pool like this:

redis_conn = redis.Redis(connection_pool=redis_pool)

此外,我假设您将hiredis 与redis-py 一起使用,因为它在某些情况下可以提高性能.您是否还检查过使用现有设置打开到 redis 服务器的连接数,因为它很可能很高?您可以使用 INFO 命令获取该信息:

Also, I am assuming you are using hiredis along with redis-py as it should improve performance in certain cases. Have you also checked the number of connections open to the redis server with your existing setup as it most likely is quite high? You can use the INFO commmand to get that information:

redis-cli info

检查 Clients 部分,您将在其中看到connected_clients"字段,该字段会告诉您此时已打开了多少个到 redis 服务器的连接.

Check for the Clients section in which you will see the "connected_clients" field that will tell you how many connections you have open to the redis server at that instant.

这篇关于如何在 redis 中正确使用连接池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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