问题
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