Streaming¶
The http_to_arrow.streaming module provides the async Arrow IPC streaming
surface. See the Streaming IPC guide for a task-oriented
walkthrough.
Tip
Import ArrowIPCStream and IPCStreamSink from http_to_arrow rather than
from their implementation modules.
Async stream¶
streaming
¶
Async Arrow IPC streaming for http_to_arrow.
ArrowIPCStream bridges an async row producer (for example an HTTP paginator)
to an Arrow IPC byte stream. Rows are buffered into an
:class:~http_to_arrow.main.ArrowRecordContainer, completed batches are
serialized to Arrow IPC stream bytes via
:class:~http_to_arrow._ipc.IPCStreamSink, and each serialized batch is released
immediately so peak memory stays close to a single batch.
A bounded :class:asyncio.Queue decouples the producer from the consumer so a
fast paginator never outruns batch serialization. Each :class:ArrowIPCStream
instance is single-use: create a fresh stream per response.
ArrowIPCStream(schema, *, batch_size=128000, queue_maxsize=4, compression=None, dictionary_encode=False, dictionary_cardinality_threshold=0.5, eager_clear_accumulator=True)
¶
Stream Arrow IPC bytes from an async row producer with backpressure.
Streaming requires an explicit schema because the Arrow IPC stream header is
written before any rows are available. Inferred-schema mode and
dictionary_encode=True are rejected because they can change batch types
after the header schema is fixed.
Example
stream = ArrowIPCStream(schema=schema, compression="zstd")
async def produce() -> None: async for page in fetch_pages(): await stream.extend(page)
async for chunk in stream.ipc_chunks(producer=produce): ... # forward chunk to the HTTP response
Source code in src/http_to_arrow/src/http_to_arrow/streaming.py
put(row)
async
¶
extend(rows)
async
¶
end()
async
¶
ipc_chunks(producer=None)
async
¶
Yield Arrow IPC stream byte chunks until the producer signals end.
When producer is provided it is run as a concurrent task and this
method signals :meth:end on its behalf, so the producer never needs to
call it. Otherwise rows must be fed from a separate task via
:meth:put/:meth:extend, and that task must call :meth:end when done.
If the producer raises, the partial stream is flushed and closed cleanly and the exception is re-raised after the trailing bytes are emitted.
Source code in src/http_to_arrow/src/http_to_arrow/streaming.py
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 | |
IPC stream sink¶
_ipc
¶
Incremental Arrow IPC stream serialization for http_to_arrow.
IPCStreamSink wraps :func:pyarrow.ipc.new_stream around a
:class:~http_to_arrow._drain.DrainableOutputStream so that each written
RecordBatch can be serialized to Arrow IPC stream bytes and yielded
immediately, keeping peak memory close to a single batch.
IPCStreamSink(schema, *, compression=None)
¶
Serialize RecordBatch objects into Arrow IPC stream byte chunks.
The schema is written into the stream header on construction; call
:meth:header_bytes to retrieve it, :meth:write_batch for each batch, and
:meth:close to emit the end-of-stream marker.
Example
sink = IPCStreamSink(schema, compression="zstd") chunks = [sink.header_bytes()] chunks.append(sink.write_batch(batch)) chunks.append(sink.close()) table = pa.ipc.open_stream(pa.BufferReader(b"".join(chunks))).read_all()
Source code in src/http_to_arrow/src/http_to_arrow/_ipc.py
closed
property
¶
Whether the stream writer has been closed.
header_bytes()
¶
Return the Arrow IPC stream header bytes captured at construction.
PyArrow may buffer the schema message until the first write_batch or
close, so this can be empty. The bytes are always emitted in stream
order, so concatenating header_bytes() with every write_batch
result and close produces a valid Arrow IPC stream.
Source code in src/http_to_arrow/src/http_to_arrow/_ipc.py
write_batch(batch)
¶
Serialize batch and return its Arrow IPC stream bytes.
Source code in src/http_to_arrow/src/http_to_arrow/_ipc.py
close()
¶
Close the writer and return the trailing end-of-stream bytes.