title: Run code after flask application has started
body: <p>My goal is to get arbitrary code to run after my Flask application is started. Here is what I've got:</p>
  
  <pre><code>def run():
      from webapp import app
      app.run(debug=True, use_reloader=False)
  </code></pre>
  
  <p>Ideally I would be able to just do this:</p>
  
  <pre><code>def run():
      from webapp import app
      app.run(debug=True, use_reloader=False)
      some_code()
  </code></pre>
  
  <p>But the code doesn't continue past <code>app.run()</code>, so some_code() never runs.</p>
  
  <p>The solution I'm working on at the moment is to run some_code() in a separate thread from app.run(), create a <a href="http://flask.pocoo.org/docs/0.10/api/#flask.Flask.before_first_request" rel="noreferrer">before first request</a> function that sets this: </p>
  
  <pre><code>app.is_running = True
  </code></pre>
  
  <p>Then get some_code() to shoot a basic request to app so that the 'before first request' code runs. This is fairly convoluted and going to be hard to document. I would rather use app.is_running parameter which already is provided in Flask, or use a <code>@app.after_server_start</code> decorator, but to my knowledge neither of those exists.</p>
  
  <p>Help me make this code better?</p>
  
  <hr>
  
  <p>Posthumous: Every time I think about this issue, it makes me wish that a <code>@app.after_server_start</code> decorator existed.</p>