Schedules the given callback to be called periodically. The callback is called every ``callback_time`` milliseconds when ``callback_time`` is a float. Note that the timeout is given in milliseconds, while most other time-related functions in Tornado use seconds. ``callback_time`` ma
| 836 | |
| 837 | |
| 838 | class PeriodicCallback(object): |
| 839 | """Schedules the given callback to be called periodically. |
| 840 | |
| 841 | The callback is called every ``callback_time`` milliseconds when |
| 842 | ``callback_time`` is a float. Note that the timeout is given in |
| 843 | milliseconds, while most other time-related functions in Tornado use |
| 844 | seconds. ``callback_time`` may alternatively be given as a |
| 845 | `datetime.timedelta` object. |
| 846 | |
| 847 | If ``jitter`` is specified, each callback time will be randomly selected |
| 848 | within a window of ``jitter * callback_time`` milliseconds. |
| 849 | Jitter can be used to reduce alignment of events with similar periods. |
| 850 | A jitter of 0.1 means allowing a 10% variation in callback time. |
| 851 | The window is centered on ``callback_time`` so the total number of calls |
| 852 | within a given interval should not be significantly affected by adding |
| 853 | jitter. |
| 854 | |
| 855 | If the callback runs for longer than ``callback_time`` milliseconds, |
| 856 | subsequent invocations will be skipped to get back on schedule. |
| 857 | |
| 858 | `start` must be called after the `PeriodicCallback` is created. |
| 859 | |
| 860 | .. versionchanged:: 5.0 |
| 861 | The ``io_loop`` argument (deprecated since version 4.1) has been removed. |
| 862 | |
| 863 | .. versionchanged:: 5.1 |
| 864 | The ``jitter`` argument is added. |
| 865 | |
| 866 | .. versionchanged:: 6.2 |
| 867 | If the ``callback`` argument is a coroutine, and a callback runs for |
| 868 | longer than ``callback_time``, subsequent invocations will be skipped. |
| 869 | Previously this was only true for regular functions, not coroutines, |
| 870 | which were "fire-and-forget" for `PeriodicCallback`. |
| 871 | |
| 872 | The ``callback_time`` argument now accepts `datetime.timedelta` objects, |
| 873 | in addition to the previous numeric milliseconds. |
| 874 | """ |
| 875 | |
| 876 | def __init__( |
| 877 | self, |
| 878 | callback: Callable[[], Optional[Awaitable]], |
| 879 | callback_time: Union[datetime.timedelta, float], |
| 880 | jitter: float = 0, |
| 881 | ) -> None: |
| 882 | self.callback = callback |
| 883 | if isinstance(callback_time, datetime.timedelta): |
| 884 | self.callback_time = callback_time / datetime.timedelta(milliseconds=1) |
| 885 | else: |
| 886 | if callback_time <= 0: |
| 887 | raise ValueError("Periodic callback must have a positive callback_time") |
| 888 | self.callback_time = callback_time |
| 889 | self.jitter = jitter |
| 890 | self._running = False |
| 891 | self._timeout = None # type: object |
| 892 | |
| 893 | def start(self) -> None: |
| 894 | """Starts the timer.""" |
| 895 | # Looking up the IOLoop here allows to first instantiate the |
no outgoing calls