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?
Solution 1:
Try next_run_time=datetime.now()
.
Solution 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')