sunflare.engine module¶
Status
¶
Bases: Status
Track the status of a potentially-lengthy action like moving or triggering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
The amount of time to wait before marking the Status as failed. If
|
None
|
settle_time
|
float | None
|
The amount of time to wait between the caller specifying that the status has completed to running callbacks. Default is 0. |
0
|
Notes
Theory of operation:
This employs two threading.Event objects, one thread that runs for
(timeout + settle_time) seconds, and one thread that runs for
settle_time seconds (if settle_time is nonzero).
At init time, a timeout and settle_time are specified. A thread
is started, on which user callbacks, registered after init time via
add_callback, will eventually be run. The thread waits on an
Event be set or (timeout + settle_time) seconds to pass, whichever
happens first.
If (timeout + settle_time) expires and the Event has not
been set, an internal Exception is set to StatusTimeoutError, and a
second Event is set, marking the Status as done and failed. The
callbacks are run.
If a callback is registered after the Status is done, it will be run immediately.
If the first Event is set before (timeout + settle_time) expires, then the second Event is set and no internal Exception is set, marking the Status as done and successful. The callbacks are run.
There are two methods that directly set the first Event. One,
set_exception, sets it directly after setting the internal
Exception. The other, set_finished, starts a
threading.Timer that will set it after a delay (the settle_time).
One of these methods may be called, and at most once. If one is called
twice or if both are called, InvalidState is raised. If they are
called too late to prevent a StatusTimeoutError, they are ignored
but one call is still allowed. Thus, an external callback, e.g. pyepics,
may reports success or failure after the Status object has expired, but
to no effect because the callbacks have already been called and the
program has moved on.
Source code in src/sunflare/engine/_status.py
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 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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
timeout
property
¶
The timeout for this action.
This is set when the Status is created, and it cannot be changed.
settle_time
property
¶
A delay between when set_finished is when the Status is done.
This is set when the Status is created, and it cannot be changed.
done
property
¶
Boolean indicating whether associated operation has completed.
This is set to True at init time or by calling
set_finished, set_exception.
Once True, it can never become False.
success
property
¶
Boolean indicating whether associated operation has completed.
This is set to True at init time or by calling
set_finished, set_exception
. Once True, it can never become False.
callbacks
property
¶
Callbacks to be run when the status is marked as finished.
set_exception
¶
Mark as finished but failed with the given Exception.
This method should generally not be called by the recipient of this Status object, but only by the object that created and returned it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc
|
BaseException
|
The exception that caused the failure. |
required |
Source code in src/sunflare/engine/_status.py
set_finished
¶
Mark as finished successfully.
This method should generally not be called by the recipient of this Status object, but only by the object that created and returned it.
Source code in src/sunflare/engine/_status.py
exception
¶
Return the exception raised by the action.
If the action has completed successfully, return None. If it has
finished in error, return the exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
If None (default) wait indefinitely until the status finishes. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Exception |
BaseException | None
|
The exception raised by the action. If the action has completed
successfully, return |
Raises:
| Type | Description |
|---|---|
WaitTimeoutError
|
If the status has not completed within |
Source code in src/sunflare/engine/_status.py
wait
¶
Block until the action completes.
When the action has finished succesfully, return None. If the
action has failed, raise the exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
If |
None
|
Raises:
| Type | Description |
|---|---|
WaitTimeoutError
|
If the status has not completed within |
StatusTimeoutError
|
If the status has failed because the timeout that it was initialized with has expired. |
Exception
|
This is |
Source code in src/sunflare/engine/_status.py
add_callback
¶
Register a callback to be called once when the Status finishes.
The callback will be called exactly once. If the Status is finished before a callback is added, it will be called immediately. This is threadsafe. The callback will be called regardless of success of failure. The callback has access to this status object, so it can distinguish success or failure by inspecting the object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback
|
Callable[[Status], None]
|
required |
Source code in src/sunflare/engine/_status.py
RunEngine
¶
Bases: RunEngine
Subclass of bluesky.run_engine.RunEngine to allow execution in a separate thread.
Suppressed features:
context_managers: The context managers are forced to be an empty list to avoid the use of the built-inSignalHandlercontext manager.
The rationale is that the original implementation is meant for interactive usage (e.g., Jupyter notebooks, scripts) and not for applications relying on an event loop.
pause_msg: Overridden to be an empty string.during_task: Overridden toDuringTask, so theRunEnginedoes not interact with any possible event loop in the main thread.
For the original class initializer signature, refer to the bluesky.run_engine.RunEngine documentation.
Source code in src/sunflare/engine/_wrapper.py
resume
¶
Resume the paused plan in a separate thread.
If the plan has been paused, the initial
future returned by __call__ will be set as completed.
With this method, the plan is resumed in a separate thread, and a new future is returned.
Returns:
| Type | Description |
|---|---|
``Future[RunEngineResult | tuple[str, ...]]``
|
Future object representing the result of the resumed plan. |