管理从 Python 到 redis 的连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12967107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:17:36  来源:igfitidea点击:

Managing connection to redis from Python

pythonconnectionredis

提问by jeruki

I'm using redis-pyin my python application to store simple variables or lists of variables in a Redis database, so I thought it would be better to create a connection to the redis server everytime I need to save or retrieve a variable as this is not done very often and don't want to have a permanent connection that might timeout.

redis-py在我的 python 应用程序中使用在 Redis 数据库中存储简单的变量或变量列表,所以我认为每次需要保存或检索变量时创建到 redis 服务器的连接会更好,因为这没有完成经常并且不希望有可能超时的永久连接。

Reading through some basic tutorials I created the connections using the Redis class, but have not found a way to close the connection, as this is the first time I'm using Redis. I'm not sure if I'm using the best approach for managing the connections so I would like some advice for this. This is how I'm setting or getting a variable now:

通读了一些基本教程,我使用 Redis 类创建了连接,但还没有找到关闭连接的方法,因为这是我第一次使用 Redis。我不确定我是否正在使用最好的方法来管理连接,所以我想为此提供一些建议。这就是我现在setting 或getting 变量的方式:

import redis

def getVariable(variable_name):
    my_server = redis.Redis("10.0.0.1")
    response = my_server.get(variable_name)
    return response

def setVariable(variable_name, variable_value):
    my_server = redis.Redis("10.0.0.1")
    my_server.set(variable_name, variable_value)

I basically use this code to store the last connection time or to get an average of requests per second done to my app and stuff like that.

我基本上使用此代码来存储上次连接时间或获得每秒对我的应用程序完成的平均请求等等。

Thanks for your advice.

谢谢你的建议。

采纳答案by Didier Spezia

Python uses a reference counter mechanism to deal with objects, so at the end of the blocks, the my_server object will be automatically destroyed and the connection closed. You do not need to close it explicitly.

Python 使用引用计数器机制来处理对象,因此在块的末尾, my_server 对象将被自动销毁并关闭连接。您不需要明确关闭它。

Now this is not how you are supposed to manage Redis connections. Connecting/disconnecting for each operation is too expensive, so it is much better to maintain the connection opened. With redis-py it can be done by declaring a pool of connections:

现在这不是您应该如何管理 Redis 连接。每次操作连接/断开连接成本太高,因此保持连接打开要好得多。使用 redis-py 可以通过声明一个连接池来完成:

import redis

POOL = redis.ConnectionPool(host='10.0.0.1', port=6379, db=0)

def getVariable(variable_name):
    my_server = redis.Redis(connection_pool=POOL)
    response = my_server.get(variable_name)
    return response

def setVariable(variable_name, variable_value):
    my_server = redis.Redis(connection_pool=POOL)
    my_server.set(variable_name, variable_value)

Please note connection pool management is mostly automatic and done within redis-py.

请注意连接池管理大多是自动的,并在 redis-py 中完成。

回答by haren

@sg1990 what if you have 10.000 users requiring redis at the same time? They cannot share a single connection and you've just created yourself a bottleneck.

@sg1990 如果您有 10.000 个用户同时需要 redis 怎么办?他们不能共享一个连接,而您刚刚给自己制造了一个瓶颈。

With a pool of connections you can create an arbitrary number of connections and simply use get_connection()and release(), from redis-py docs.

随着连接池,您可以创建连接任意数量的和简单的使用get_connection()release(),从Redis的-PY文档

A connection per user is a huge overkill, since every connection needs to maintain an open socket. This way you'd automatically decrease a number of e.g. concurrent websocket users that your machine can handle by half.

每个用户的连接是一个巨大的矫枉过正,因为每个连接都需要维护一个打开的套接字。通过这种方式,您会自动将您的机器可以处理的并发 websocket 用户数量减少一半。