nidaqlib.streaming¶
nidaqlib.streaming ¶
Streaming acquisition surface.
Two recorders, one per acquisition model (design doc §11.3 / §13.1):
- :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.
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 / monotonic_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.
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 (stream, summary). The stream is closed when this context
manager exits; summary is mutated in place during the run and is
safe to read after exit (the values are frozen on the way out).
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
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 | |
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 (stream, summary). The per-tick payload depends on
source:
- :class:
DaqSession→ one :class:DaqReadingper tick. - :class:
DaqManager→Mapping[str, DeviceResult[DaqReading]]per tick (matches :meth:DaqManager.poll).
summary is updated in place during the run; a final snapshot is
frozen on exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
DaqSession | DaqManager
|
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
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 | |