title: How to run recurring task in the Python Flask framework?
body: <p>I'm building a website which provides some information to the visitors. This information is aggregated in the background by polling a couple external APIs every 5 seconds. The way I have it working now is that I use <a href="http://apscheduler.readthedocs.org/en/latest/" rel="noreferrer">APScheduler</a> jobs. I initially preferred APScheduler because it makes the whole system more easy to port (since I don't need to set cron jobs on the new machine). I start the polling functions as follows:</p>
  
  <pre><code>from apscheduler.scheduler import Scheduler
  
  @app.before_first_request
  def initialize():
      apsched = Scheduler()
      apsched.start()
  
      apsched.add_interval_job(checkFirstAPI, seconds=5)
      apsched.add_interval_job(checkSecondAPI, seconds=5)
      apsched.add_interval_job(checkThirdAPI, seconds=5)
  </code></pre>
  
  <p>This kinda works, but there's some trouble with it: </p>
  
  <ol>
  <li>For starters, this means that the interval-jobs are running outside of the Flask context. So far this hasn't been much of a problem, but when calling an endpoint fails I want the system to send me an email (saying "hey calling API X failed"). Because it doesn't run within the Flask context however, it complaints that <a href="https://pythonhosted.org/flask-mail/" rel="noreferrer">flask-mail</a> cannot be executed (<code>RuntimeError('working outside of application context')</code>).</li>
  <li>Secondly, I wonder how this is going to behave when I don't use the Flask built-in debug server anymore, but a production server with lets say 4 workers. Will it start every job four times then?</li>
  </ol>
  
  <p>All in all I feel that there should be a better way of running these recurring tasks, but I'm unsure how. Does anybody out there have an interesting solution to this problem? All tips are welcome!</p>
  
  <p>[EDIT]
  I've just been reading about <a href="http://www.celeryproject.org/" rel="noreferrer">Celery</a> with its <a href="http://celery.readthedocs.org/en/latest/reference/celery.schedules.html" rel="noreferrer">schedules</a>. Although I don't really see how Celery is different from APScheduler and whether it could thus solve my two points, I wonder if anyone reading this thinks that I should investigate more in Celery?</p>
  
  <p>[CONCLUSION]
  About two years later I'm reading this, and I thought I could let you guys know what I ended up with. I figured that @BluePeppers was right in saying that I shouldn't be tied so closely to the Flask ecosystem. So I opted for regular cron-jobs running every minute which are set using Ansible. Although this makes it a bit more complex (I needed to learn Ansible and convert some code so that running it every minute would be enough) I think this is more robust. 
  I'm currently using the awesome <a href="http://python-rq.org/" rel="noreferrer">pythonr-rq</a> for queueing a-sync jobs (checking APIs and sending emails). I just found out about <a href="https://github.com/ui/rq-scheduler" rel="noreferrer">rq-scheduler</a>. I haven't tested it yet, but it seems to do precisely what I needed in the first place. So maybe this is a tip for future readers of this question.</p>
  
  <p>For the rest, I just wish all of you a beautiful day!</p>