60 lines
1.4 KiB
Python
60 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
from threading import Timer
|
|
|
|
"""
|
|
Timer module that looptydoops the entire software
|
|
"""
|
|
|
|
__author__ = "@tormakris"
|
|
__copyright__ = "Copyright 2020, Birbnetes Team"
|
|
__module_name__ = "loopingtimer"
|
|
__version__text__ = "1"
|
|
|
|
|
|
class LoopingTimer:
|
|
"""
|
|
Looping timer. Executes a function and repeates it in the given interval.
|
|
"""
|
|
def __init__(self, interval, function, tick_args=None, tick_kwargs=None):
|
|
"""
|
|
Initialize class
|
|
:param interval:
|
|
:param function:
|
|
:param tick_args:
|
|
:param tick_kwargs:
|
|
"""
|
|
self._timer = None
|
|
self.interval = interval
|
|
self.function = function
|
|
self._tick_args = tick_args or []
|
|
self._tick_kwargs = tick_kwargs or {}
|
|
self.is_running = False
|
|
self.start()
|
|
|
|
def _run(self) -> None:
|
|
"""
|
|
Run once
|
|
:return:
|
|
"""
|
|
self.is_running = False
|
|
self.start()
|
|
self.function(*self._tick_args, **self._tick_kwargs)
|
|
|
|
def start(self) -> None:
|
|
"""
|
|
Start running
|
|
:return:
|
|
"""
|
|
if not self.is_running:
|
|
self._timer = Timer(self.interval, self._run)
|
|
self._timer.start()
|
|
self.is_running = True
|
|
|
|
def stop(self) -> None:
|
|
"""
|
|
Stop running
|
|
:return:
|
|
"""
|
|
self._timer.cancel()
|
|
self.is_running = False
|