Source: src/cwsandbox/_types.py:384
Sync and async writer for streaming input to a process.
StreamWriter wraps a bounded asyncio.Queue and provides both synchronous and
asynchronous write interfaces, so you can send streaming input in
both sync and async contexts.
The stream uses None as a sentinel value to signal end-of-stream (EOF).
The queue is bounded (~16 items for ~1MB with 64KB chunks) to provide
backpressure.
Attributes
QUEUE_SIZE : Default: 16.
Properties
closed
True if close() has been called.
Methods
write
Write raw bytes to the stream.
Queues the data for sending to the process stdin. Blocks (via OperationRef.result())
if the queue is full, providing backpressure.
Parameters
data (bytes): The bytes to write.
Returns
OperationRef[None]: An OperationRef that completes when the data is queued.
Raises
SandboxExecutionError: If the stream is closed or has failed.
writeline
Write a line of text to the stream.
Encodes the text, appends a newline, and queues it for sending.
Parameters
text (str): The text to write.
encoding (str): The text encoding to use. Defaults to “utf-8”.
Returns
OperationRef[None]: An OperationRef that completes when the data is queued.
Raises
SandboxExecutionError: If the stream is closed or has failed.
close
Close the stream, sending EOF sentinel.
The EOF sentinel is queued at the end, so pending writes complete first.
Multiple calls to close() are idempotent and return immediately.
Returns
OperationRef[None]: An OperationRef that completes when EOF is queued.
set_exception
Store an exception to raise on subsequent writes.
Called internally when the stream fails (for example, when the process exits).
Parameters
exception (BaseException): The exception to store.
Last modified on May 29, 2026