-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
37 lines (29 loc) · 865 Bytes
/
utils.py
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
"""
Common utilities used in this project.
"""
__author__ = 'Holger Fleischmann'
__copyright__ = 'Copyright 2018, Holger Fleischmann, Bavaria/Germany'
__license__ = 'Apache License 2.0'
from threading import Thread
from threading import Event
import time
from typing import Callable
class RepeatTimer(Thread):
"""
Repeating timer that calls a callback task() at regular interval seconds.
"""
interval: float
task: Callable[[], None]
event: Event
def __init__(self, interval: float, task: Callable[[], None]):
super().__init__()
self.interval = interval
self.task = task
self.event = Event()
self.event.set()
def run(self) -> None:
while self.event.is_set():
self.task()
time.sleep(self.interval)
def cancel(self) -> None:
self.event.clear()