modal.io_streams

modal.io_streams.StreamReader

class StreamReader(object)

Provides an interface to buffer and fetch logs from a stream (stdout or stderr).

As an asynchronous iterable, the object supports the async for statement.

Usage

from modal import Sandbox

sandbox = Sandbox.create(
    "bash",
    "-c",
    "for i in $(seq 1 10); do echo foo; sleep 0.1; done",
    app=app,
)
for message in sandbox.stdout:
    print(f"Message: {message}")

file_descriptor

@property
def file_descriptor(self):

read

def read(self) -> str:

Fetch and return contents of the entire stream. If EOF was received, return an empty string.

Usage

from modal import Sandbox

sandbox = Sandbox.create("echo", "hello", app=app)
sandbox.wait()

print(sandbox.stdout.read())

modal.io_streams.StreamWriter

class StreamWriter(object)

Provides an interface to buffer and write logs to a sandbox or container process stream (stdin).

def __init__(self, object_id: str, object_type: Literal["sandbox", "container_process"], client: _Client):

write

def write(self, data: Union[bytes, bytearray, memoryview]):

Writes data to stream’s internal buffer, but does not drain/flush the write.

This method needs to be used along with the drain() method which flushes the buffer.

Usage

from modal import Sandbox

sandbox = Sandbox.create(
    "bash",
    "-c",
    "while read line; do echo $line; done",
    app=app,
)
sandbox.stdin.write(b"foo\n")
sandbox.stdin.write(b"bar\n")
sandbox.stdin.write_eof()

sandbox.stdin.drain()
sandbox.wait()

write_eof

def write_eof(self):

Closes the write end of the stream after the buffered write data is drained. If the process was blocked on input, it will become unblocked after write_eof().

This method needs to be used along with the drain() method which flushes the EOF to the process.

drain

def drain(self):

Flushes the write buffer and EOF to the running process.