Trigger alias for
IntervalTrigger(weeks=0, days=0, hours=0, minutes=0, seconds=0, start_date=None, end_date=None, timezone=None, jitter=None)¶
Triggers on specified intervals, starting on
datetime.now()+ interval otherwise.
- weeks (int) – number of weeks to wait
- days (int) – number of days to wait
- hours (int) – number of hours to wait
- minutes (int) – number of minutes to wait
- seconds (int) – number of seconds to wait
- start_date (datetime|str) – starting point for the interval calculation
- end_date (datetime|str) – latest possible date/time to trigger on
- timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations
- jitter (int|None) – advance or delay the job execution by
jitterseconds at most.
This method schedules jobs to be run periodically, on selected intervals.
You can also specify the starting date and ending dates for the schedule through the
parameters, respectively. They can be given as a date/datetime object or text (in the
ISO 8601 format).
If the start date is in the past, the trigger will not fire many times retroactively but instead calculates the next run time from the current time, based on the past start time.
from datetime import datetime from apscheduler.schedulers.blocking import BlockingScheduler def job_function(): print("Hello World") sched = BlockingScheduler() # Schedule job_function to be called every two hours sched.add_job(job_function, 'interval', hours=2) sched.start()
You can use
end_date to limit the total time in which the schedule runs:
# The same as before, but starts on 2010-10-10 at 9:30 and stops on 2014-06-15 at 11:00 sched.add_job(job_function, 'interval', hours=2, start_date='2010-10-10 09:30:00', end_date='2014-06-15 11:00:00')
scheduled_job() decorator works nicely too:
from apscheduler.scheduler import BlockingScheduler @sched.scheduled_job('interval', id='my_job_id', hours=2) def job_function(): print("Hello World")
jitter option enables you to add a random component to the execution time. This might be useful if you have
multiple servers and don’t want them to run a job at the exact same moment or if you want to prevent multiple jobs
with similar options from always running concurrently:
# Run the `job_function` every hour with an extra-delay picked randomly in a [-120,+120] seconds window. sched.add_job(job_function, 'interval', hours=1, jitter=120)