forked from spdconvos/encryptedbot_py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.py
40 lines (30 loc) · 1002 Bytes
/
Set.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
38
39
40
import logging
import threading
import time
log = logging.getLogger(__name__)
class Interval:
"""A class that loops every interval."""
def __init__(self, interval: float, action) -> None:
"""Initialize the interval loop.
Args:
interval (float): The interval in seconds.
action (function): The action to do.
"""
self.interval = interval
self.action = action
self.stop = threading.Event()
thread = threading.Thread(target=self._setInterval)
thread.start()
def _setInterval(self) -> None:
"""Does things."""
next = time.time()
while not self.stop.wait(next - time.time()):
next += self.interval
try:
self.action()
except OSError as e:
log.exception(e)
pass
def cancel(self) -> None:
"""Cancels the interval."""
self.stop.set()