-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_timer.py
More file actions
192 lines (158 loc) · 6.91 KB
/
py_timer.py
File metadata and controls
192 lines (158 loc) · 6.91 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Copyright 2014 Sean Donovan
# List-based timer that only has one timer active at a time. When a new timer
# request comes in, the timer will insert into the list at the appropriate
# location. If it's to pop soonest, it will stop the current timer, and start a
# timer for the newest time. This has only one timer outstanding at a time,
# rather than starting a timer for each one.
#
# This should be used by using the following import statement:
# from py_timer import py_timer as Timer
# This will remap Timer to the new timer class.
import logging
from datetime import datetime, timedelta
from threading import Timer, Lock, Thread
import pprint
class py_timer_manager:
INSTANCE = None
def __init__(self):
if self.INSTANCE is not None:
raise ValueError("Instance already exists!")
self.list_of_inactive_timers = []
self.list_of_active_timers = []
self.thread_timer = None
self.timerlist_lock = Lock()
@classmethod
def get_instance(cls):
if cls.INSTANCE is None:
cls.INSTANCE = py_timer_manager()
return cls.INSTANCE
def insert_into_list(self, timer):
# New inactive timer
self.list_of_inactive_timers.append(timer)
def start_timer(self, timer):
# Move a timer from the inactive list
first = False
self.list_of_inactive_timers.remove(timer)
timer.calculate_expiration()
# Insert based on time to expire. O(n) time.
if len(self.list_of_active_timers) == 0:
self.list_of_active_timers.insert(0, timer)
first = True
else:
inserted = False
for x in range(len(self.list_of_active_timers)):
if self.list_of_active_timers[x].expiration > timer.expiration:
self.list_of_active_timers.insert(x, timer)
inserted = True
if x == 0:
first = True
break
if inserted == False:
self.list_of_active_timers.append(timer)
# If the timer is the next to expire, stop the running timer
if first == True:
if self.thread_timer is not None:
self.thread_timer.cancel()
# Call _restart_timer()
self._restart_timer()
def is_timer_alive(self, timer):
return (timer in self.list_of_active_timers)
def remove_from_list(self, timer):
with self.timerlist_lock:
if timer in self.list_of_inactive_timers:
self.list_of_inactive_timers.remove(timer)
elif timer in self.list_of_active_timers:
# If it's the first in the list_of_active_timers, need to do extra
if self.list_of_active_timers[0] == timer:
self.thread_timer.cancel()
self.list_of_active_timers.remove(timer)
self._restart_timer()
else:
self.list_of_active_timers.remove(timer)
# else: trying to cancel an already cancelled timer shouldn't blow up.
def _restart_timer(self):
'''
This is what happens when things expire, the timer needs to be stopped
due to removal of entries, or insertion of new entries at the beginning
of the list_of_active_timers, etc. Pretty much the go-to function.
'''
with self.timerlist_lock:
if self.thread_timer is not None:
self.thread_timer.cancel()
# Call back anything that's expired
now = datetime.now()
entries_to_remove = []
for entry in self.list_of_active_timers:
if entry.expiration <= now:
entries_to_remove.append(entry)
entry.call_function()
for entry in entries_to_remove:
self.list_of_active_timers.remove(entry)
# Start the first timer that's not expired
if len(self.list_of_active_timers) != 0:
delta = self.list_of_active_timers[0].expiration - now
self.thread_timer = Timer(delta.total_seconds(),
self._finish_timer)
# Set the daemon flag: If the main program dies, this will die too
# prevents the case where a 3 day long timer for a long lived DNS
# entry keeps the program running for ages.
self.thread_timer.daemon = True
self.thread_timer.start()
def _finish_timer(self):
'''
The timer calls back to this so that we can log things easily if need be
'''
self._restart_timer()
class py_timer:
def __init__(self, interval, function, args=[], kwargs={}):
'''
In order to behave like the current threading.Timer class, use the same
interface. the py_timer is mostly a tracking structure, while
py_timer_manager (above) works the timer magic.
interval, function, args[], kwargs{}
'''
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.expiration = None
# Get the instance of the py_timer_manager, insert into list
self.manager = py_timer_manager.get_instance()
self.manager.insert_into_list(self)
def calculate_expiration(self):
self.expiration = datetime.now() + timedelta(seconds=self.interval)
def start(self):
self.manager.start_timer(self)
def cancel(self):
self.manager.remove_from_list(self)
def is_alive(self):
self.manager.is_timer_alive(self)
def call_function(self):
cb_thread = None
if ((len(self.args) == 0) and len(self.kwargs) == 0):
cb_thread = Thread(target=self.function)
elif ((len(self.args) != 0) and len(self.kwargs) == 0):
cb_thread = Thread(target=self.function,
args=self.args)
elif ((len(self.args) == 0) and len(self.kwargs) != 0):
cb_thread = Thread(target=self.function,
kwargs=self.kwargs)
else:
cb_thread = Thread(target=self.function,
args=self.args,
kwargs=self.kwargs)
cb_thread.start()
def call_function_orig(self):
''' Calls the expiration function. Lots of splatting. '''
if (len(self.args) == 0):
pass
if (len(self.kwargs) == 0):
pass
if ((len(self.args) == 0) and len(self.kwargs) == 0):
self.function()
elif ((len(self.args) != 0) and len(self.kwargs) == 0):
self.function(*self.args)
elif ((len(self.args) == 0) and len(self.kwargs) != 0):
self.function(**self.kwargs)
else:
self.function(*self.args, **self.kwargs)