Managing connection to redis from Python

后端 未结 2 1264
耶瑟儿~
耶瑟儿~ 2020-12-12 15:06

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 co

2条回答
  •  无人及你
    2020-12-12 15:36

    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.

提交回复
热议问题