timer_queue.py¶
A simple thread safe timer queue implementation which has O(logn) time complexity.
TEARDOWN_SENTINEL = None
module-attribute
¶
__all__ = ['Timer', 'TimerQueueStruct', 'TimerQueue']
module-attribute
¶
Timer
¶
Timer wraps the callback and timestamp related attributes.
Source code in solnlib/timer_queue.py
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 |
|
ident = Timer._ident + 1
instance-attribute
¶
interval = interval
instance-attribute
¶
when = when
instance-attribute
¶
__call__()
¶
Source code in solnlib/timer_queue.py
79 80 |
|
__eq__(other)
¶
Source code in solnlib/timer_queue.py
64 65 |
|
__ge__(other)
¶
Source code in solnlib/timer_queue.py
76 77 |
|
__gt__(other)
¶
Source code in solnlib/timer_queue.py
73 74 |
|
__hash__()
¶
Source code in solnlib/timer_queue.py
61 62 |
|
__init__(callback, when, interval, ident=None)
¶
Initializes Timer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
Arbitrary callable object. |
required |
when |
int
|
The first expiration time, seconds since epoch. |
required |
interval |
int
|
Timer interval, if equals 0, one time timer, otherwise the timer will be periodically executed. |
required |
ident |
int
|
(optional) Timer identity. |
None
|
Source code in solnlib/timer_queue.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
__le__(other)
¶
Source code in solnlib/timer_queue.py
70 71 |
|
__lt__(other)
¶
Source code in solnlib/timer_queue.py
67 68 |
|
update_expiration()
¶
Source code in solnlib/timer_queue.py
58 59 |
|
TimerQueue
¶
A simple timer queue implementation.
It runs a separate thread to handle timers Note: to effectively use this timer queue, the timer callback should be short, otherwise it will cause other timers’s delay execution. A typical use scenario in production is that the timers are just a simple functions which inject themselvies to a task queue and then they are picked up by a threading/process pool to execute, as shows below:
Timers --enqueue---> TimerQueue --------expiration-----------
|
|
\|/
Threading/Process Pool <---- TaskQueue <--enqueue-- Timers' callback (nonblocking)
Examples:
>>> from solnlib import timer_queue
>>> tq = timer_queue.TimerQueue()
>>> tq.start()
>>> t = tq.add_timer(my_func, time.time(), 10)
>>> # do other stuff
>>> tq.stop()
Source code in solnlib/timer_queue.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
__init__()
¶
Source code in solnlib/timer_queue.py
218 219 220 221 222 223 224 |
|
add_timer(callback, when, interval, ident=None)
¶
Add timer to the queue.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
Arbitrary callable object. |
required |
when |
int
|
The first expiration time, seconds since epoch. |
required |
interval |
int
|
Timer interval, if equals 0, one time timer, otherwise the timer will be periodically executed |
required |
ident |
int
|
(optional) Timer identity. |
None
|
Returns:
Type | Description |
---|---|
Timer
|
A timer object which should not be manipulated directly by clients. Used to delete/update the timer. |
Source code in solnlib/timer_queue.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
remove_timer(timer)
¶
Remove timer from the queue.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timer |
Timer
|
Timer object to remove. |
required |
Source code in solnlib/timer_queue.py
268 269 270 271 272 273 274 275 276 |
|
start()
¶
Start the timer queue.
Source code in solnlib/timer_queue.py
226 227 228 229 230 231 232 233 234 |
|
stop()
¶
Stop the timer queue.
Source code in solnlib/timer_queue.py
236 237 238 239 240 241 242 243 244 |
|
TimerQueueStruct
¶
The underlying data structure for TimerQueue.
Source code in solnlib/timer_queue.py
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 |
|
__init__()
¶
Source code in solnlib/timer_queue.py
89 90 91 |
|
add_timer(callback, when, interval, ident)
¶
Add timer to the data structure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback |
Callable
|
Arbitrary callable object. |
required |
when |
int
|
The first expiration time, seconds since epoch. |
required |
interval |
int
|
Timer interval, if equals 0, one time timer, otherwise the timer will be periodically executed |
required |
ident |
int
|
(optional) Timer identity. |
required |
Returns:
Type | Description |
---|---|
Timer
|
A timer object which should not be manipulated directly by clients. Used to delete/update the timer. |
Source code in solnlib/timer_queue.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
check_and_execute()
¶
Get expired timers and execute callbacks for the timers.
Returns:
Type | Description |
---|---|
float
|
Duration of next expired timer. |
Source code in solnlib/timer_queue.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
get_expired_timers()
¶
Get a list of expired timers.
Returns:
Type | Description |
---|---|
Tuple
|
A tuple of |
Source code in solnlib/timer_queue.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
remove_timer(timer)
¶
Remove timer from data structure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timer |
Timer
|
Timer object which is returned by |
required |
Source code in solnlib/timer_queue.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
reset_timers(expired_timers)
¶
Re-add the expired periodical timers to data structure for next round scheduling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expired_timers |
List[Timer]
|
List of expired timers. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if there are timers added, False otherwise. |
Source code in solnlib/timer_queue.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|