Roopendra Roopendra - 1 month ago 19
Python Question

Can we limit APScheduler to run 100 times only?

I am using APScheduler to run a python method in every 5 minutes. It's working perfectly. I need to limit the scheduled job iteration to 100. After the 100 iteration it should close the process. I have checked the reference document but I am unable to find any option which provide this feature. We have option to control number of job instance but not the max iteration. Does anyone has idea about that?

from apscheduler.schedulers.blocking import BlockingScheduler

def job():
print "Decorated job"

scheduler = BlockingScheduler()
scheduler.add_job(job, 'interval', minutes=5)
scheduler.start()


OR
if I get the scheduled job iteration count then I can also remove the running job from code itself like below.

scheduler = BlockingScheduler()
scheduler.add_job(job, 'interval', minutes=5, id='my_job_id')

#iterationCount ??

if (iterationCount = 100 ):
scheduler.remove_job('my_job_id')
exit(0)
scheduler.start()

Answer

According to the documentation the interval trigger allows setting the time range for the job:

You can use start_date and end_date to limit the total time in which the schedule runs.

sched.add_job(job_function, 'interval', hours=2, start_date='2010-10-10 09:30:00', end_date='2014-06-15 11:00:00')

This is not exactly what you want, but it at least allows to set the end date which you can easily compute yourself.