title: Can I persist an http connection (or other data) across Flask requests?
body: <p>I'm working on a Flask app which retrieves the user's XML from the myanimelist.net API (<a href="http://myanimelist.net/malappinfo.php?u=doomcat55&amp;status=all&amp;type=anime" rel="nofollow">sample</a>), processes it, and returns some data. The data returned can be different depending on the Flask page being viewed by the user, but the initial process (retrieve the XML, create a User object, etc.) done before each request is always the same.</p>
  
  <p>Currently, retrieving the XML from myanimelist.net is the bottleneck for my app's performance and adds on a good 500-1000ms to each request. Since all of the app's requests are to the myanimelist server, I'd like to know if there's a way to persist the http connection so that once the first request is made, subsequent requests will not take as long to load. I don't want to cache the entire XML because the data is subject to frequent change.</p>
  
  <p>Here's the general overview of my app:</p>
  
  <pre><code>from flask import Flask
  from functools import wraps
  import requests
  
  app = Flask(__name__)
  
  
  def get_xml(f): 
      @wraps(f)
      def wrap():
          # Get the XML before each app function
          r = requests.get('page_from_MAL') # Current bottleneck
          user = User(data_from_r) # User object
          response = f(user)
          return response
      return wrap
  
  
  @app.route('/one')
  @get_xml
  def page_one(user_object):
      return 'some data from user_object'
  
  
  @app.route('/two')
  @get_xml
  def page_two(user_object):
      return 'some other data from user_object'
  
  if __name__ == '__main__':
      app.run()
  </code></pre>
  
  <p>So is there a way to persist the connection like I mentioned? Please let me know if I'm approaching this from the right direction.</p>