51

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 every time I need to save or retrieve a variable as this is not done very often and I don't want to have a permanent connection that timeout.

After 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.

StarGit
  • 35
  • 14
jeruki
  • 1,860
  • 3
  • 20
  • 27

3 Answers3

80

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.

Didier Spezia
  • 70,911
  • 12
  • 189
  • 154
  • thanks i didnt know about that, but if i create the connection pool do i have a method to close it any way when im out of this module or is it not necesary? – jeruki Oct 19 '12 at 15:29
  • 2
    Not necessary. Python will close the connection when the last reference to the POOL object will be destroyed (here at the end of the script). – Didier Spezia Oct 19 '12 at 15:37
  • I'm connecting to redis with an URL from my server environnement. I use redis.from_url(ENV['MYSERVER_REDIS_URL'],0). I didn't find any ConnectionPool.from_url() method. Do you know how to create a connection pool from an URL ? – Guillaume Gendre Nov 15 '12 at 13:43
  • Use urlparse function to parse the url and extract the connection parameters, as done in https://github.com/andymccurdy/redis-py/blob/master/redis/client.py#L236 – Didier Spezia Nov 15 '12 at 14:54
  • 9
    This might be because of an older version of redis.py that this answer refers to, but currently redis.py will set up a connection pool for you. No need to explicitely do that. – pors Jan 24 '14 at 12:15
  • 1
    Some on #redis feel that the cost of maintaining a connection pool is more costly than spawning new connections if the use is not that often. – dman Mar 21 '14 at 02:23
4

@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.

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

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.

haren
  • 1,595
  • 2
  • 11
  • 17
  • 1
    AFAIK connection pool is created automatically either you specify it explicitly or not. That is true for what I can see from source https://github.com/andymccurdy/redis-py/blob/master/redis/client.py#L493 – r_black Jan 12 '17 at 17:41
  • 1
    @r_black, you are correct. It means, if you create `redis.Redis` multiple times, it'll create multiple connection pools. If you provide `connection_pool` then it uses that, so you have a single connection. – Shiplu Mokaddim Mar 22 '18 at 15:33
-2

you can use this to create two databases in redis:

    r1  = redis.StrictRedis(host="localhost", port=6379, db=0, decode_responses=True)
    r2  = redis.StrictRedis(host="localhost", port=6379, db=1, decode_responses=True)
StarGit
  • 35
  • 14