Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python apschedule BlockingScheduler with interval trigger: Start immediately

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?

like image 917
user4493177 Avatar asked Apr 06 '17 11:04

user4493177


2 Answers

Try next_run_time=datetime.now().

like image 150
Alex Grönholm Avatar answered Nov 13 '22 09:11

Alex Grönholm


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')
like image 6
ybdesire Avatar answered Nov 13 '22 10:11

ybdesire