-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtimeout.py
37 lines (29 loc) · 885 Bytes
/
timeout.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
import time
class Timeout:
_running = []
def __init__(self, chat_id: int, user_id: int, msg_id: int, timer: int):
self.chat_id = chat_id
self.user_id = user_id
self.msg_id = msg_id
self._valid = False
self.timer = timer
def run(self, callback, **callback_args):
Timeout._add_to_list(self)
self._valid = True
time.sleep(self.timer)
Timeout._remove_from_list(self)
if self._valid:
result = callback(**callback_args)
self._valid = False
return result
def stop(self):
self._valid = False
@classmethod
def _add_to_list(cls, timeout):
cls._running.append(timeout)
@classmethod
def list_all(cls):
return cls._running
@classmethod
def _remove_from_list(cls, timeout):
cls._running.remove(timeout)