scheduler¶
Thread-safe priority-queue based event scheduler.
This module provides :class:Scheduler, a sched-like class that executes
actions at scheduled times using a background thread and an executor.
Events can be scheduled at absolute or relative times, cancelled by event
or reference, and queried by predicate. A process-wide :func:common_scheduler
is also provided.
Scheduler
¶
A thread-safe event scheduler executing actions via an executor.
Events are stored in a heap ordered by (time, sequence). A daemon thread pops due events and submits them for execution.
Attributes:
| Name | Type | Description |
|---|---|---|
timegen |
The time source used for now() and wait(). |
|
executor |
The executor used to run scheduled actions. |
Example
import py_misc_utils.scheduler as sch
s = sch.Scheduler()
ev = s.enter(0.01, lambda: None)
s.enterabs(s.timegen.now() + 0.02, lambda: None, ref='demo')
assert s.get_events(lambda e: e.ref == 'demo')
assert s.ref_cancel('demo')
assert s.cancel(ev)
Source code in py_misc_utils/scheduler.py
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 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 | |
gen_unique_ref()
¶
Generate a unique reference string for event cancellation.
Returns:
| Type | Description |
|---|---|
|
A UUID4-based string. |
Source code in py_misc_utils/scheduler.py
94 95 96 97 98 99 100 | |
enterabs(ts, action, ref=None, argument=(), kwargs=None)
¶
Schedule an action at an absolute timestamp.
The timestamp is interpreted in the time source of the scheduler (timegen), which by default is the wall clock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ts
|
The absolute time to run the action. |
required | |
action
|
The callable to execute. |
required | |
ref
|
Optional reference identifier for later cancellation. |
None
|
|
argument
|
Positional arguments for action. |
()
|
|
kwargs
|
Keyword arguments for action. |
None
|
Returns:
| Type | Description |
|---|---|
|
The scheduled Event instance. |
Source code in py_misc_utils/scheduler.py
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 | |
enter(delay, action, ref=None, argument=(), kwargs=None)
¶
Schedule an action after a relative delay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delay
|
Seconds from now to run the action. |
required | |
action
|
The callable to execute. |
required | |
ref
|
Optional reference identifier for later cancellation. |
None
|
|
argument
|
Positional arguments for action. |
()
|
|
kwargs
|
Keyword arguments for action. |
None
|
Returns:
| Type | Description |
|---|---|
|
The scheduled Event instance. |
Source code in py_misc_utils/scheduler.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
cancel(event)
¶
Cancel one or more scheduled events by identity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
An Event instance or list/tuple of Event instances. |
required |
Returns:
| Type | Description |
|---|---|
|
The list of cancelled Event instances. |
Source code in py_misc_utils/scheduler.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
ref_cancel(ref)
¶
Cancel events by their reference identifier.
Example
import py_misc_utils.scheduler as sch
s = sch.Scheduler()
s.enter(60.0, lambda: None, ref='cleanup')
assert s.ref_cancel('cleanup')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref
|
A reference string or list/tuple of reference strings. |
required |
Returns:
| Type | Description |
|---|---|
|
The list of cancelled Event instances. |
Source code in py_misc_utils/scheduler.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
get_events(fn)
¶
Return queued events matching a predicate.
Only events not yet due - still sitting in the queue - are returned: once popped, an event is handed to the executor and disappears from the scheduler queue even if its action is still running.
Example
import py_misc_utils.scheduler as sch
s = sch.Scheduler()
ev = s.enter(60.0, lambda: None)
assert s.get_events(lambda e: e.ref is None) == [ev]
s.cancel(ev)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
A callable taking an Event and returning True if it matches. |
required |
Returns:
| Type | Description |
|---|---|
|
A list of matching Event instances currently in the queue. |
Source code in py_misc_utils/scheduler.py
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 | |
common_scheduler()
¶
Return the process-wide common Scheduler instance, creating it if needed.
Returns:
| Type | Description |
|---|---|
|
The shared Scheduler backed by the common executor. |
Source code in py_misc_utils/scheduler.py
253 254 255 256 257 258 259 260 261 262 263 264 265 | |