title: What's the right approach for calling functions after a flask app is run?
body: <p>I'm a little confused about how to do something that I thought would be quite simple. I have a simple app written using <code>Flask</code>. It looks something like this:</p>
  
  <pre><code>from flask import Flask
  
  app = Flask(__name__)
  
  def _run_on_start(a_string):
      print "doing something important with %s" % a_string
  
  @app.route('/')
  def root():
      return 'hello world'
  
  if __name__ == "__main__":
      if len(sys.argv) &lt; 2:
          raise Exception("Must provide domain for application execution.")
      else:
          DOM = sys.argv[1]
          _run_on_start("%s" % DOM)
          app.run(debug=True)
  </code></pre>
  
  <p>What I'm finding is that my terminal is outputting the print statements in <code>_run_on_start</code> but non of the other usual Flask app debug code. If I remove the call before app.run, the output is normal. Further I'm finding the output of <code>_run_on_start</code> to be repeated twice on startup, though I don't know if it's some weird output or the function is actually being called twice.</p>
  
  <p>I'm assuming this is not the right way to add a function call before you call <code>app.run</code>. I looked in the Flask docs and found mentions of various decorators one can use, which allow  you to execute a function before/after certain requests but I want to execute the call when the app server is run.</p>
  
  <p>Further, I realise that if I call this module from another module, i.e., not when <code>__name__ != "__main__"</code> my I won't get my call to <code>_run_on_start</code>. </p>
  
  <p>What's the right approach here? In both cases when I'm starting from the CL and from another module?</p>