apscheduler in Flask executes twice


I have problem when i am using apscheduler in my flask application.

In my view.py file i am writing like this

import time
from apscheduler.scheduler import Scheduler

def test_scheduler():
     print "TEST"
     print time.time()

sched = Scheduler()
sched.add_interval_job(test_scheduler, seconds=5)

And then this method test_scheduler() executes twice in every five second

TEST 1360844314.01 TEST 1360844314.2

2/14/2013 12:22:32 PM

Accepted Answer

I've made it, I added in add_interval_job parameter to start after a certain time point

sched.add_interval_job(test_scheduler, seconds=5, start_date='2013-02-13 00:00')
2/14/2013 1:16:44 PM

In debug mode, Flask's reloader will load the flask app twice (How to stop Flask from initialising twice in Debug Mode?). I'm not sure why this is, but it causes apscheduler's jobs to be scheduled twice. A quick print "loaded scheduler" right before sched.start() confirms this.

There are a couple ways around this, as mentioned in the linked answer. The one I found that worked best is just to disable the reloader like so:


It means I have to reload my app manually as I develop it, but it's a small price to pay to get apscheduler working.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow