Skip to main content

Streaming History Read

Overview​

IDE When loading historical data for a trend chart, loading ten thousand data points all at once list can cause the process to lag OOM.read_history_stream Yes.Asynchronous Generator, It yields one item at a time OpcUaDataValue; the business logic side fetches async for one item at a time to process it, rendering as it goes. It is an instance method of OpcUaSession (also known as DarraOpcUa).

Corresponding section of the standard: OPC UA Part 11(Historical Access) §6.5 (HistoryRead).

API​

MethodsCategoryReading and WritingNote
ua.read_history_stream(node_id, start_utc, end_utc, batch_size)HistoryReadAsynchronous Stream Reading (Asynchronous Generator)
async def read_history_stream(self,
node_id: str,
start_utc: datetime,
end_utc: datetime,
batch_size: int = 1000):
"""逐条 yield OpcUaDataValue."""
ParametersDefaultNote
node_id—Historical Milestones; Empty String Exception ValueError
start_utc—Start UTC (datetime)
end_utc—End UTC; <= start_utc Ends immediately; does not yield
batch_size1000Maximum RPC Points Per Round (Suggested Value; see "Implementation Limitations")

Returns: Asynchronous Generator, yield line by line ( OpcUaDataValue not DataValue). Each line is executed by the caller / dispose() close(), or directly with dv:.

Code Example​

import asyncio
from datetime import datetime, timedelta, timezone

from darra_opcua import DarraOpcUa # Module Name darra_opcua (pip package darra-opcua)


async def main():
with DarraOpcUa("opc.tcp://localhost:4840") as ua:
ua.connect()

t1 = datetime.now(timezone.utc)
t0 = t1 - timedelta(hours=24)

# 1) Render as you go
async for dv in ua.read_history_stream("ns=2;s=Temp", t0, t1):
with dv:
# dv.variant.as_double Retrieve the value of `typed` (Optional[float]); `dv.value` is the converted Python value
chart_series.add_point(dv.source_timestamp, dv.variant.as_double)

# 2) Early exit — Use a direct `break`; no further values are yielded.
async for dv in ua.read_history_stream("ns=2;s=Temp", t0, t1):
with dv:
process(dv)
if stop_requested:
break

# 3) Counting bad values
bad = 0
async for dv in ua.read_history_stream("ns=2;s=Temp", t0, t1):
with dv:
if not dv.is_good: # dv.status returns an enumeration StatusCode
bad += 1
print(f"24h 内 {bad} 个坏值")


asyncio.run(main())

Implementation Constraints​

  • Currently, read_history no points are exposed ContinuationPoint; a single call RPC returns all points within the interval (subject to the batch_size limit).
  • batch_size It's just a hint about "the maximum number of points to retrieve at a time"; the underlying implementation still returns all points at once.
  • Business units in large regions can slice their own data
  • The parameter was not removed — If you want to stop early, do it within the loop break, but the underlying iteration has already RPC finished sending everything.

Best Practices​

  • Long-running queries should be able to be stopped early; this break can be done within a loop (there is no cancel token to pass).
  • For time intervals greater than 1 hour, it is recommended to use outer slices.
  • UI Render Throttling: Every 100 refreshes

Cross-Language Comparison​

C#PythonJavaC++RustC
ReadHistoryStreamAsyncread_history_streamreadHistoryStreamReadHistoryStreamread_history_streamDarraUa_HistoryStream_New/_Next/_IsEnd/_Free

Next Step​