Skip to main content
Source: src/cwsandbox/_types.py:384
class StreamWriter(queue: asyncio.Queue[bytes | None], loop_manager: _LoopManager)
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

@property
def closed(self) -> bool
True if close() has been called.

Methods

write

write(data: bytes) -> OperationRef[None]
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

writeline(text: str, encoding: str = 'utf-8') -> OperationRef[None]
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() -> OperationRef[None]
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

set_exception(exception: BaseException) -> None
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