How do I schedule an interval job with APScheduler?

后端 未结 5 2041
情话喂你
情话喂你 2020-12-19 05:32

I\'m trying to schedule an interval job with APScheduler (v3.0.0).

I\'ve tried:

from apscheduler.schedulers.blocking import BlockingScheduler
sched =         


        
相关标签:
5条回答
  • 2020-12-19 05:42

    The documentation had an error there. I've fixed it now. That first line should be:

    from apscheduler.schedulers.blocking import BlockingScheduler
    

    It would've raised an ImportError though, but you didn't mention any. Did you try any of the provided examples?

    0 讨论(0)
  • 2020-12-19 05:45

    You need to keep the thread alive. Here is a example of how I used it.

    from subprocess import call
    
    import time
    import os
    
    from apscheduler.schedulers.background import BackgroundScheduler
    
    
    def job():
        print("In job")
        call(['python', 'scheduler/main.py'])
    
    
    if __name__ == '__main__':
        scheduler = BackgroundScheduler()
        scheduler.configure(timezone=utc)
        scheduler.add_job(job, 'interval', seconds=10)
        scheduler.add
        scheduler.start()
        print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
    
        try:
            # This is here to simulate application activity (which keeps the main thread alive).
            while True:
                time.sleep(5)
        except (KeyboardInterrupt, SystemExit):
            # Not strictly necessary if daemonic mode is enabled but should be done if possible
            scheduler.shutdown()
    
    0 讨论(0)
  • 2020-12-19 05:49

    I haven't figured out what caused the original issue, but I got around it by swapping the order in which the jobs are scheduled, so that the 'interval' job is scheduled BEFORE the 'cron' jobs.

    i.e. I switched from this:

    def my_cron_job1():
        print "cron job 1"
    
    def my_cron_job2():
        print "cron job 2"
    
    def my_interval_job():
        print "interval job"
    
    if __name__ == '__main__':
        from apscheduler.schedulers.blocking import BlockingScheduler
        sched = BlockingScheduler(timezone='MST')
    
        sched.add_job(my_cron_job1, 'cron', id='my_cron_job1', minute=10)
        sched.add_job(my_cron_job2, 'cron', id='my_cron_job2', minute=20)
    
        sched.add_job(my_interval_job, 'interval', id='my_job_id', seconds=5)
    

    to this:

    def my_cron_job1():
        print "cron job 1"
    
    def my_cron_job2():
        print "cron job 2"
    
    def my_interval_job():
        print "interval job"
    
    if __name__ == '__main__':
        from apscheduler.schedulers.blocking import BlockingScheduler
        sched = BlockingScheduler(timezone='MST')
    
        sched.add_job(my_interval_job, 'interval', id='my_job_id', seconds=5)
    
        sched.add_job(my_cron_job1, 'cron', id='my_cron_job1', minute=10)
        sched.add_job(my_cron_job2, 'cron', id='my_cron_job2', minute=20)
    

    and now both the cron jobs and the interval jobs run without a problem in both environments.

    0 讨论(0)
  • 2020-12-19 05:49

    How can the next wakeup be due 254 seconds from now when the interval job is set to 5 seconds??

    It's simple: you have many pending executions as your most of the jobs didn't completed in the interval-window of time.

    You could use the following parameters in order to sort this out:

     **misfire_grace_time**:    Maximum time in seconds for the job execution to be allowed to delay before it is considered a misfire 
     **coalesce**:  Roll several pending executions of jobs into one
    

    To read more, check the documentation here.

    0 讨论(0)
  • 2020-12-19 05:52

    Ok, I've looked at the updated question.

    The reason you're having problems may be that you could be using the wrong timezone. Your country is currently using daylight saving time in most locations, so the correct timezone would probably be MDT (Mountain Daylight Time). But that will break again when you move back to standard time. So I advise you to use a timezone like "America/Denver". That will take care of the DST switches.

    Question: Are you using CentOS? So far it's the only known operating system where automatic detection of the local timezone is impossible.

    0 讨论(0)
提交回复
热议问题