从Python管理与redis的连接 [英] Managing connection to redis from Python

查看:243
本文介绍了从Python管理与redis的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的python应用程序中使用 redis-py 来存储Redis数据库中的简单变量或变量列表,所以我认为最好创建一个连接到redis服务器每次我需要保存或检索一个变量,因为这是不经常做,不想有永久连接可能超时。



通过一些基本教程阅读我创建了使用Redis类的连接,但没有找到一个方法来关闭连接,因为这是我第一次使用Redis。我不知道如果我使用最好的方法来管理连接,所以我想要一些建议。
这是我如何设置 ting或 get 现在设置变量:

  import redis 

def getVariable(variable_name):
my_server = redis.Redis(10.0.0.1 )
response = my_server.get(variable_name)
返回响应

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



我基本上使用这个代码来存储最后连接时间,或获得我的应用程序和这样的东西的平均每秒请求。



感谢您的建议。


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



现在这不是你应该如何管理Redis连接。每个操作的连接/断开过于昂贵,因此更好的是保持连接打开。使用redis-py可以通过声明一个连接池来实现:

  import redis 

= 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)
返回响应

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

请注意,连接池管理大多是自动的, -py。


I'm using redis-py in 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.

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:

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.

解决方案

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.

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:

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.

这篇关于从Python管理与redis的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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