nidaqlib.streaming¶
nidaqlib.streaming ¶
Streaming acquisition surface.
Two recorders, one per acquisition model:
- :func:
record— hardware-clocked block path, emits :class:DaqBlock. - :func:
record_polled— software-timed scalar path, emits :class:DaqReading.
Don't unify them; they have different correctness models.
Both yield a :class:Recording[T] from their context manager. The
Recording wraps the payload stream, the live :class:AcquisitionSummary,
and the active rate_hz so consumers can read all three from one object.
AcquisitionSummary
dataclass
¶
AcquisitionSummary(
blocks_emitted=0,
blocks_dropped=0,
errors_observed=0,
started_at=(lambda: datetime.now(UTC))(),
finished_at=None,
)
Per-run counters, yielded alongside the block stream.
Mirrors sartoriuslib.AcquisitionSummary shape but is intentionally
mutable: counters are updated in place during the run so consumers
can poll progress (e.g. for a TUI bar) and read final counts after
exit. The recorder is the only writer; consumers MUST treat the
object as read-only.
Attributes:
| Name | Type | Description |
|---|---|---|
blocks_emitted |
int
|
Total :class: |
blocks_dropped |
int
|
Records dropped because of an
:class: |
errors_observed |
int
|
Wrapped NI errors seen during the run, regardless
of :class: |
started_at |
datetime
|
Wall-clock at recorder entry. |
finished_at |
datetime | None
|
Wall-clock at recorder exit. |
ErrorPolicy ¶
Bases: StrEnum
How recorders react to wrapped NI errors during a read.
RAISE
class-attribute
instance-attribute
¶
Cancel the recorder's task group and re-raise the error.
RETURN
class-attribute
instance-attribute
¶
Emit a :class:DaqBlock (or :class:DaqReading) with .error set,
then continue.
The recorder MUST advance timing counters (block_index /
first_sample_index / t_mono_ns) on error records so consumers
can detect dropped intervals. Consumers MUST gate on error is None
before reading data.
OverflowPolicy ¶
Bases: StrEnum
Behaviour when the recorder's outbound stream is full.
BLOCK
class-attribute
instance-attribute
¶
Producer awaits consumer. Risks NI buffer overrun on hardware-clocked tasks.
DROP_NEWEST
class-attribute
instance-attribute
¶
Drop the about-to-be-enqueued block. Bounds consumer latency; loses freshest data.
DROP_OLDEST
class-attribute
instance-attribute
¶
Drop the oldest queued block. Keeps newest data; loses older queued blocks.
PollSource ¶
Bases: Protocol
Anything that yields per-name :class:DeviceResult[DaqReading] per call.
Same name across all four sibling libraries. The recorder layer's
:func:record_polled accepts any :class:PollSource instance,
decoupling the polled producer from the concrete session/manager
types.
poll
async
¶
Read once across the named resources (or all, when names is None).
PollSourceAdapter ¶
Wrap a polled :class:DaqSession as a :class:PollSource.
Multi-channel by design: one DAQ task covers many channels, so the
returned mapping has exactly one entry — keyed by the task name —
carrying a multi-channel :class:DaqReading. Individual channels stay
inside reading.values.
Example::
adapter = PollSourceAdapter(session)
async with record_polled(adapter, rate_hz=2.0) as recording:
async for results in recording.stream:
reading = results[session.spec.name].value
...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
DaqSession
|
A started :class: |
required |
Source code in src/nidaqlib/streaming/poll_source.py
poll
async
¶
Read one :class:DaqReading and wrap it as {name: DeviceResult.success(reading)}.
names is accepted for Protocol uniformity. When provided, the
adapter only emits a row if the session's name appears in
names — otherwise the returned mapping is empty.
Source code in src/nidaqlib/streaming/poll_source.py
Recording
dataclass
¶
Active-recording handle returned by :func:record / :func:record_polled.
Attributes:
| Name | Type | Description |
|---|---|---|
stream |
AsyncIterator[T]
|
Async iterator of payloads. Closes when the recorder context manager exits. |
summary |
AcquisitionSummary
|
Mutable :class: |
rate_hz |
float | None
|
Configured cadence of the active recording. |
record
async
¶
record(
source,
*,
chunk_size,
timeout=10.0,
buffer_size=16,
error_policy=ErrorPolicy.RAISE,
overflow=OverflowPolicy.DROP_OLDEST,
use_callback_bridge=False,
)
Hardware-clocked block acquisition.
Yields a :class:Recording[DaqBlock]. The stream is closed when this
context manager exits; summary is mutated in place during the run
and is safe to read after exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
DaqSession
|
A configured :class:
|
required |
chunk_size
|
int
|
Samples per channel per emitted :class: |
required |
timeout
|
float
|
Per-read timeout in seconds (Option A only — Option B reads from the NI buffer with timeout 0). |
10.0
|
buffer_size
|
int
|
AnyIO memory-object stream buffer, in :class: |
16
|
error_policy
|
ErrorPolicy
|
:attr: |
RAISE
|
overflow
|
OverflowPolicy
|
Backpressure policy. |
DROP_OLDEST
|
use_callback_bridge
|
bool
|
Opt into the §11.3.2 every-N-samples callback
path. Default |
False
|
Raises:
| Type | Description |
|---|---|
NIDaqTaskStateError
|
|
ValueError
|
|
Source code in src/nidaqlib/streaming/block.py
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 | |
record_polled
async
¶
record_polled(
source,
*,
rate_hz,
error_policy=ErrorPolicy.RAISE,
overflow=OverflowPolicy.BLOCK,
buffer_size=64,
)
Software-timed scalar polling at rate_hz.
Yields a :class:Recording[T]. The per-tick payload type T depends
on source:
- :class:
DaqSession→ one :class:DaqReadingper tick. - :class:
DaqManager→Mapping[str, DeviceResult[DaqReading]]per tick (matches :meth:DaqManager.poll). - Any :class:
PollSource(including :class:PollSourceAdapter) →Mapping[str, DeviceResult[DaqReading]]returned by itspoll().
summary is updated in place during the run; a final snapshot is
frozen on exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
DaqSession | DaqManager | PollSource
|
A started :class: |
required |
rate_hz
|
float
|
Target poll rate, in Hz. Must be > 0. |
required |
error_policy
|
ErrorPolicy
|
:attr: |
RAISE
|
overflow
|
OverflowPolicy
|
Backpressure policy. Defaults to :attr: |
BLOCK
|
buffer_size
|
int
|
AnyIO send-stream capacity in payload slots. |
64
|
Raises:
| Type | Description |
|---|---|
NIDaqTaskStateError
|
A session |
ValueError
|
|
Source code in src/nidaqlib/streaming/recorder.py
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 | |