sgn.sources
¶
Source elements for generating data streams.
New classes need not be subclassed from IterSource or DequeSource, but should at least be ultimately a subclass of SourceElement.
DequeSource
dataclass
¶
Bases: IterSource
flowchart TD
sgn.sources.DequeSource[DequeSource]
sgn.sources.IterSource[IterSource]
sgn.base.SourceElement[SourceElement]
sgn.base.ElementLike[ElementLike]
sgn.base.UniqueID[UniqueID]
sgn.sources.IterSource --> sgn.sources.DequeSource
sgn.base.SourceElement --> sgn.sources.IterSource
sgn.base.ElementLike --> sgn.base.SourceElement
sgn.base.UniqueID --> sgn.base.ElementLike
click sgn.sources.DequeSource href "" "sgn.sources.DequeSource"
click sgn.sources.IterSource href "" "sgn.sources.IterSource"
click sgn.base.SourceElement href "" "sgn.base.SourceElement"
click sgn.base.ElementLike href "" "sgn.base.ElementLike"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
A source element that has one double-ended-queue (deque) per source pad.
.. deprecated::
DequeSource is deprecated and will be removed in a future release.
For new code prefer :class:IterSource; if dynamic appending is
required, pass a deque (or any iterable) via iters and consume
it with the iterator protocol. See the migration note below.
Ordering is LIFO, not FIFO. Values are produced via deque.pop()
(right-most first), so iters={"H1": deque([1, 2, 3])} yields
3, 2, 1. To enqueue new data at runtime in producer-order use
deques[name].appendleft(x) (or, equivalently, prefer
:class:IterSource which consumes in insertion order).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iters
|
dict[str, Iterable[Any]] | None
|
dict[str, deque ], a mapping of source pads to deque s, where the key is the pad name and the value is the deque |
None
|
eos_on_empty
|
dict[str, bool] | bool
|
dict[str, bool] | bool, default True, a mapping of source pads to boolean values, where the key is the pad name and the value is the boolean. If a bool is given, the value is applied to all pads. If True, EOS is signaled when the deque is empty. |
True
|
Source code in src/sgn/sources.py
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 | |
deques
property
¶
Get the iters property with more explicit alias.
IterSource
dataclass
¶
Bases: SourceElement
flowchart TD
sgn.sources.IterSource[IterSource]
sgn.base.SourceElement[SourceElement]
sgn.base.ElementLike[ElementLike]
sgn.base.UniqueID[UniqueID]
sgn.base.SourceElement --> sgn.sources.IterSource
sgn.base.ElementLike --> sgn.base.SourceElement
sgn.base.UniqueID --> sgn.base.ElementLike
click sgn.sources.IterSource href "" "sgn.sources.IterSource"
click sgn.base.SourceElement href "" "sgn.base.SourceElement"
click sgn.base.ElementLike href "" "sgn.base.ElementLike"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
A source element that has one iterable per source pad.
On each call to :meth:new one value is pulled from the per-pad
iterator and wrapped in a frame. When an iterator is exhausted the
next frame produced for that pad has data=None and is_gap=True;
EOS is signaled on that same frame iff eos_on_empty is True for
the pad (the default).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iters
|
dict[str, Iterable[Any]] | None
|
dict[str, Iterable[Any]], a mapping of source pads to iterables, where the key is the pad name and the value is the Iterable. These will be coerced to iterators, so they can be any iterable type. |
None
|
eos_on_empty
|
dict[str, bool] | bool
|
dict[str, bool] | bool, default True, a mapping of source pads to boolean values, where the key is the pad name and the value is the boolean. If a bool is given, the value is applied to all pads. If True, EOS is signaled when the iterator is empty. |
True
|
Source code in src/sgn/sources.py
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 | |
__post_init__()
¶
Post init checks for the IterSource element.
new(pad)
¶
New Frames are created on "pad" with an instance specific count and a name derived from the pad name. EOS is set if we have surpassed the requested number of Frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
SourcePad
|
SourcePad, the pad for which to produce a new Frame |
required |
Returns:
| Type | Description |
|---|---|
Frame
|
Frame, the Frame with optional data payload |
Source code in src/sgn/sources.py
update(pad)
¶
Update the iterator for the pad. This is a no-op for IterSource. For subclasses that need to update the iterator, this method should be overridden. Examples include reading from a file or network stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
SourcePad
|
SourcePad, the pad to update |
required |
Source code in src/sgn/sources.py
NullSource
dataclass
¶
Bases: SourceElement, SignalEOS
flowchart TD
sgn.sources.NullSource[NullSource]
sgn.base.SourceElement[SourceElement]
sgn.base.ElementLike[ElementLike]
sgn.base.UniqueID[UniqueID]
sgn.sources.SignalEOS[SignalEOS]
sgn.base.SourceElement --> sgn.sources.NullSource
sgn.base.ElementLike --> sgn.base.SourceElement
sgn.base.UniqueID --> sgn.base.ElementLike
sgn.sources.SignalEOS --> sgn.sources.NullSource
click sgn.sources.NullSource href "" "sgn.sources.NullSource"
click sgn.base.SourceElement href "" "sgn.base.SourceElement"
click sgn.base.ElementLike href "" "sgn.base.ElementLike"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
click sgn.sources.SignalEOS href "" "sgn.sources.SignalEOS"
A source that does precisely nothing.
It is useful for testing and debugging, and will always produce empty frames
frame_factory: Callable = Frame
wait: float = None
num_frames: int = None
If wait is not None the source will block for wait seconds before each new buffer, which is useful for slowing down debugging pipelines. By default this source element handles SIGINT and uses that to set EOS. See SignalEOS. In order to use this feature, the pipeline must be run within the SignalEOS context manager, e.g.,
with SignalEOS() as signal_eos:
p.run()
Source code in src/sgn/sources.py
new(pad)
¶
New Frames are created on "pad" with an instance specific count and a name derived from the pad name. EOS is set if we have surpassed the requested number of Frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
SourcePad
|
SourcePad, the pad for which to produce a new Frame |
required |
Returns:
| Type | Description |
|---|---|
Frame
|
Frame, the Frame with optional data payload |
Source code in src/sgn/sources.py
SignalEOS
¶
This class provides global signal handling for an SGN pipeline. If you inherit it for a source element then it will capture SIGINT and provide a method to mark that eos should be flagged. See NullSource as an example.
Additionally this must be used as a context manager for executing a pipeline and disabling the signal hander after the pipeline is done, e.g.,
with SignalEOS() as signal_eos:
p.run()
Note: signal.signal only delivers signals on the main thread of the
main interpreter. If a source's new runs from a worker thread the
handler installed here will not fire there.
Source code in src/sgn/sources.py
__enter__()
¶
Store the previous signal handlers and setup new ones for the handled signals
Source code in src/sgn/sources.py
__exit__(exc_type, exc_val, exc_tb)
¶
Restore the original signal handlers
raise_signal(sig)
¶
Raise a signal that has already been raised previously.
Intended to be used if the application needs to re-raise one of the signals with the previous signal handler. NOTE - this will only raise the signal if it had been previously raised and only within a given context.
Source code in src/sgn/sources.py
signaled_eos()
classmethod
¶
Indicate whether a signal has been received to indicate an EOS.
Returns true of the intersection of received signals and handled signals is nonzero. This can be used by developers to decide if EOS should be set.
Source code in src/sgn/sources.py
StatsSource
dataclass
¶
Bases: SourceElement
flowchart TD
sgn.sources.StatsSource[StatsSource]
sgn.base.SourceElement[SourceElement]
sgn.base.ElementLike[ElementLike]
sgn.base.UniqueID[UniqueID]
sgn.base.SourceElement --> sgn.sources.StatsSource
sgn.base.ElementLike --> sgn.base.SourceElement
sgn.base.UniqueID --> sgn.base.ElementLike
click sgn.sources.StatsSource href "" "sgn.sources.StatsSource"
click sgn.base.SourceElement href "" "sgn.base.SourceElement"
click sgn.base.ElementLike href "" "sgn.base.ElementLike"
click sgn.base.UniqueID href "" "sgn.base.UniqueID"
A source element that produces system statistics.
This source collects system statistics using psutil and produces frames containing system performance data for the current SGN pipeline and system.
Frame data semantics:
- When stats are collected,
datais a dict withtimestamp, and (optionally)process/systemkeys. - When
intervalis set and the window has not yet elapsed, the frame carries an empty dict (data == {}) and is markedis_gap=Trueso downstream consumers can distinguish "no sample this tick" from a real measurement.
Signal-driven EOS: when eos_on_signal=True (the default) this source
consults :class:SignalEOS to decide whether to flag EOS, but it does
not install signal handlers on its own. The pipeline must be run
inside the :class:SignalEOS context manager (with SignalEOS(): ...)
or EOS-on-signal will silently never fire.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float | None
|
float | None, time in seconds between stats collection. If None, stats are collected every time new() is called. |
None
|
include_process_stats
|
bool
|
bool, whether to include statistics about the current process. |
True
|
include_system_stats
|
bool
|
bool, whether to include system-wide statistics. |
True
|
frame_factory
|
Callable
|
Callable, the factory function to create frames. |
Frame
|
eos_on_signal
|
bool
|
bool, whether to end the stream on receiving a signal (SIGINT/SIGTERM). |
True
|
wait
|
float | None
|
float | None, time in seconds to wait between frames. If None, frames are produced as fast as possible. |
None
|
Source code in src/sgn/sources.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | |
__post_init__()
¶
Post initialization setup for StatsSource.
Source code in src/sgn/sources.py
check_eos()
¶
Check if end-of-stream has been signaled.
Returns:
| Type | Description |
|---|---|
bool
|
bool, True if EOS should be set. |
Source code in src/sgn/sources.py
new(pad)
¶
Create a new Frame containing system statistics.
This method is called by the pipeline to produce a new frame with current system statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pad
|
SourcePad
|
SourcePad, the pad for which to produce a new Frame |
required |
Returns:
| Type | Description |
|---|---|
Frame
|
Frame, the Frame containing system statistics |
Source code in src/sgn/sources.py
should_collect_stats()
¶
Back-compat alias for :meth:_maybe_advance_collection_window.
The name reads as a pure predicate but the call mutates
_last_collection_time; the underscore-prefixed method is the
preferred spelling in new code.