title: Managing connection to redis from Python
body: <p>I'm using <code>redis-py</code> 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.</p>
  <p>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 <code>set</code>ting or <code>get</code>ting a variable now:</p>
  <pre><code>import redis
  
  def getVariable(variable_name):
      my_server = redis.Redis(&quot;10.0.0.1&quot;)
      response = my_server.get(variable_name)
      return response
  
  def setVariable(variable_name, variable_value):
      my_server = redis.Redis(&quot;10.0.0.1&quot;)
      my_server.set(variable_name, variable_value)
  </code></pre>
  <p>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.</p>
  <p>Thanks for your advice.</p>