pythonschedulerblockingweb.pyapscheduler

python apscheduler not consistent


I'm running a scheduler using python apscheduler inside web.py framework. The function runserver is supposed to run everyday at 9 a.m but it is inconsistent. It runs most days but skips a day once in a while.

Code:

import web
from apscheduler.schedulers.blocking import BlockingScheduler #Blocking Scheduler

#URLs
urls = (
    '/startscheduler/','index',
    )

Nightlysched = BlockingScheduler()

@Nightlysched.scheduled_job('cron', hour=9)
def runserver():
    print 2+2 #doing some calculations here

#Main function to run the cron JOB
if __name__ == "__main__":
    Nightlysched.start() #stating the job
    app = web.application(urls, globals())
    app.run() 

What is the correct way to configure the scheduler to run every day at 9.a.m?


Solution

  • APScheduler has a grace period during which jobs are allowed to run. If for some reason the scheduler is busy and/or the load of the host is too high, APScheduler might fail to start the job in time.

    In this cases, the job will be discarded if it could not be started during the grace time (an explanatory message will be logged if you have initialized Python logging).

    Depending on the actual root cause:

    In summary, I'd first try with misfire_grace_period:

    @Nightlysched.scheduled_job('cron', hour=9, misfire_grace_time=None)
    

    As a note, though, as @Alex mentioned I don't grasp why your code works, because the call to Nightlysched.start() should be blocking and preventing your web application from running. I guess this is pasted code and doesn't really represent what you are running. To me, it looks like you should instead be using a non-blocking scheduler like BackgroundScheduler.