summaryrefslogtreecommitdiff
path: root/docs/modules/triggers/interval.rst
blob: 1f74c3ab980ddcbb1dc2d1ba7a32aba330a9d288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
:mod:`apscheduler.triggers.interval`
====================================

.. automodule:: apscheduler.triggers.interval

API
---

Trigger alias for :meth:`~apscheduler.schedulers.base.BaseScheduler.add_job`: ``interval``

.. autoclass:: IntervalTrigger
    :show-inheritance:


Introduction
------------

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 ``start_date`` and ``end_date``
parameters, respectively. They can be given as a date/datetime object or text (in the
`ISO 8601 <https://en.wikipedia.org/wiki/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.


Examples
--------

::

    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 ``start_date`` and ``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)


The :meth:`~apscheduler.schedulers.base.BaseScheduler.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")