python apschedule BlockingScheduler with interval trigger: Start immediately

断了今生、忘了曾经 提交于 2020-12-29 13:13:29

问题


I am using python apscheduler to schedule a specific task every 45 minutes. The problem is, when i add the job and start the scheduler, it starts at 45 minutes from now.

from apscheduler.schedulers.blocking import BlockingScheduler

class myClass:

    def schedule(self):
        self.scheduler = BlockingScheduler()
        self.scheduler.add_job(self.myJob, 'interval', minutes=45)
        self.scheduler.start()

    def myJob(self):
        print('I finally started')

I tried setting start_date, but with no success. How can i make sure the job is executed immediately, and not after waiting the interval for the first time?


回答1:


Try next_run_time=datetime.now().




回答2:


Not a good solution but works for me.

from apscheduler.schedulers.blocking import BlockingScheduler

class myClass:

    def schedule(self):
        self.myJob()#run your job immediately here, then scheduler
        self.scheduler = BlockingScheduler()
        self.scheduler.add_job(self.myJob, 'interval', minutes=45)
        self.scheduler.start()

    def myJob(self):
        print('I finally started')


来源:https://stackoverflow.com/questions/43254531/python-apschedule-blockingscheduler-with-interval-trigger-start-immediately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!