Skip to content

Sessions and streams

fastsandbox.Session

Session(native: SessionGuard)

A session that keeps a sandbox running while it is held.

Use it as an async context manager, or call release(); the sandbox stops about 30 s after the last session closes.

sandbox property

sandbox: str

The name of the sandbox.

is_connected property

is_connected: bool

Whether the session is still open.

release

release() -> None

Closes the session.

fastsandbox.Interactive

Interactive(native: ExecSession)

An interactive command or agent session.

Write its standard input with write and close_stdin, read its output with async for chunk in session, resize its terminal, wait for its exit code; or collect everything at once. As an async context manager, it closes the standard input on exit.

id property

id: str

The id of the execution.

write async

write(data: bytes | str) -> None

Writes data (UTF-8 for a string) to the standard input.

close_stdin async

close_stdin() -> None

Closes the standard input: the command reads end of file.

resize async

resize(width: int, height: int) -> None

Resizes the terminal of a TTY session.

inspect async

inspect() -> ExecInspect

The state of the execution.

wait async

wait(timeout: float = 60.0) -> int

Waits for the exit code, for at most timeout seconds.

collect async

collect(stdin: bytes | str = b'', exit_timeout: float = 10.0) -> AttachOutput

Writes stdin, closes it, and reads everything until the exit.

Only possible before the session is used piece by piece.

fastsandbox.Stream

Stream(native: ItemStream, convert: Callable[[Any], T])

A stream of the daemon: daemon events, prompts, creation or pull progress, export chunks.

The chunks of an image export (client.images.export_stream) are a Stream[bytes].

Iterate it with async for; leave early with aclose() or by using it as an async context manager, which closes its connection to the daemon.

aclose async

aclose() -> None

Stops the stream and closes its connection.

fastsandbox.SshTunnel

SshTunnel(native: SshTunnel)

A byte tunnel to the SSH server of a sandbox.

The server accepts any user, root included, without credentials: whoever holds the tunnel is root in the sandbox. Keep it to trusted code.

read async

read(size: int = 65536) -> bytes

Reads at most size bytes; b"" is the end of the stream.

write async

write(data: bytes) -> None

Writes data to the tunnel.

close async

close() -> None

Closes the tunnel.

proxy_stdio async

proxy_stdio() -> None

Relays the tunnel to the standard input and output of the process.

The job of an OpenSSH ProxyCommand (see fastsandbox ssh-proxy).