| 1 |
:mod:`apscheduler.triggers.interval` |
| 2 |
==================================== |
| 3 |
|
| 4 |
.. automodule:: apscheduler.triggers.interval |
| 5 |
|
| 6 |
API |
| 7 |
--- |
| 8 |
|
| 9 |
Trigger alias for :meth:`~apscheduler.schedulers.base.BaseScheduler.add_job`: ``interval`` |
| 10 |
|
| 11 |
.. autoclass:: IntervalTrigger |
| 12 |
:show-inheritance: |
| 13 |
|
| 14 |
|
| 15 |
Introduction |
| 16 |
------------ |
| 17 |
|
| 18 |
This method schedules jobs to be run periodically, on selected intervals. |
| 19 |
|
| 20 |
You can also specify the starting date and ending dates for the schedule through the ``start_date`` and ``end_date`` |
| 21 |
parameters, respectively. They can be given as a date/datetime object or text (in the |
| 22 |
`ISO 8601 <https://en.wikipedia.org/wiki/ISO_8601>`_ format). |
| 23 |
|
| 24 |
If the start date is in the past, the trigger will not fire many times retroactively but instead calculates the next |
| 25 |
run time from the current time, based on the past start time. |
| 26 |
|
| 27 |
|
| 28 |
Examples |
| 29 |
-------- |
| 30 |
|
| 31 |
:: |
| 32 |
|
| 33 |
from datetime import datetime |
| 34 |
|
| 35 |
from apscheduler.schedulers.blocking import BlockingScheduler |
| 36 |
|
| 37 |
|
| 38 |
def job_function(): |
| 39 |
print("Hello World") |
| 40 |
|
| 41 |
sched = BlockingScheduler() |
| 42 |
|
| 43 |
# Schedule job_function to be called every two hours |
| 44 |
sched.add_job(job_function, 'interval', hours=2) |
| 45 |
|
| 46 |
sched.start() |
| 47 |
|
| 48 |
|
| 49 |
You can use ``start_date`` and ``end_date`` to limit the total time in which the schedule runs:: |
| 50 |
|
| 51 |
# The same as before, but starts on 2010-10-10 at 9:30 and stops on 2014-06-15 at 11:00 |
| 52 |
sched.add_job(job_function, 'interval', hours=2, start_date='2010-10-10 09:30:00', end_date='2014-06-15 11:00:00') |
| 53 |
|
| 54 |
|
| 55 |
The :meth:`~apscheduler.schedulers.base.BaseScheduler.scheduled_job` decorator works nicely too:: |
| 56 |
|
| 57 |
|
| 58 |
@sched.scheduled_job('interval', id='my_job_id', hours=2) |
| 59 |
def job_function(): |
| 60 |
print("Hello World") |
| 61 |
|
| 62 |
|
| 63 |
The ``jitter`` option enables you to add a random component to the execution time. This might be useful if you have |
| 64 |
multiple servers and don't want them to run a job at the exact same moment or if you want to prevent multiple jobs |
| 65 |
with similar options from always running concurrently:: |
| 66 |
|
| 67 |
# Run the `job_function` every hour with an extra delay picked randomly between 0 and 120 seconds. |
| 68 |
sched.add_job(job_function, 'interval', hours=1, jitter=120) |