title: Python Redis connection should be closed on every request? (flask)
body: <p><em>I am creating flask app with Redis database. And I have one connection question</em></p>
  
  <p><strong>I can have Redis connection global and keep non-closed all time:</strong></p>
  
  <p><em><strong>init</strong>.py</em></p>
  
  <pre><code>import os
  from flask import Flask
  import redis
  
  app = Flask(__name__)
  
  db = redis.StrictRedis(host='localhost', port=6379, db=0)
  </code></pre>
  
  <p><strong>Also  I can reconnect every request (Flask doc <a href="http://flask.pocoo.org/docs/tutorial/dbcon/" rel="noreferrer">http://flask.pocoo.org/docs/tutorial/dbcon/</a>):</strong></p>
  
  <p><em><strong>init</strong>.py</em></p>
  
  <pre><code>import os
  from flask import Flask
  import redis
  
  app = Flask(__name__)
  
  #code...
  
  @app.before_request
  def before_request():
      g.db = connect_db()
  
  @app.teardown_request
  def teardown_request(exception):
      db = getattr(g, 'db', None)
      if db is not None:
          db.close()
  </code></pre>
  
  <p>Which method is better? Why I should use it?</p>
  
  <p><em><strong>Thanks for the help!</em></strong></p>