The examples on this page omit
auth. To authenticate with your W&B API key, install cwsandbox[wandb] and pass auth=AuthStrategy.WANDB to Sandbox.run(), Sandbox(), or Sandbox.session(). See Get started.exec() method. Use it when you need to run shell commands, scripts, or interactive interpreters inside a sandbox and want to capture their output, stream it in real time, send input on stdin, or control how non-zero exit codes are handled. The guide is for developers who already work with the cwsandbox Python client.
Run a basic command
Start with a single command that runs to completion and returns its captured output. Theexec() method returns a Process handle:
Get results
exec() returns immediately so you can decide how to wait for the command. Call .result() on the Process handle to block for the output:
Stream output
Blocking on.result() waits for the command to finish before you see any output. To observe progress as it happens, iterate over process.stdout before calling .result():
- Monitor long-running processes.
- Process output as it arrives.
- Implement progress indicators.
Stream input to stdin
Some commands need input written to stdin while they run, such as pipelines, interactive interpreters, or tools that read until EOF. Send input to running commands by enabling stdin withstdin=True:
StreamWriter methods
Whenstdin=True, process.stdin is a StreamWriter with three methods:
write(data: bytes): Write raw bytes. ReturnsOperationRef[None].writeline(text: str): Write text with a trailing newline (encodes to UTF-8). ReturnsOperationRef[None].close(): Signal EOF. The system completes pending writes first. ReturnsOperationRef[None].
stdin=False (the default), process.stdin is None.
Send multiple writes
Send data incrementally before closing:Run interactive Python through stdin
Feed Python code to an interactive interpreter:Combine stdin and stdout streaming
Stream output while sending input:Handle EOF-dependent commands
Some commands (likesort) read all input before producing output. Close stdin to signal EOF:
Use stdin in async contexts
In async contexts,await each OperationRef directly:
When to use stdin=True compared with stdin=False
Set the working directory
By default, commands run from the sandbox’s default working directory. Override that withcwd:
Set a timeout
To stop a command that might freeze or run longer than you expect, set a timeout withtimeout_seconds:
Handle errors with check
Thecheck parameter controls error behavior for non-zero exit codes:
Default behavior with check=False
Returns the result regardless of exit code:Raise on failure with check=True
RaisesSandboxExecutionError on non-zero exit:
Run Python code
Sandboxes are commonly used to run Python code, either as short one-liners or as longer scripts passed inline:Sequential compared with parallel execution
Choose sequential execution when later commands depend on earlier ones, and parallel execution when commands are independent and you want them to run concurrently across sandboxes.Run commands sequentially when order matters
Run independent commands in parallel
Wait for N of M processes to complete
Usecwsandbox.wait() to wait for a subset of processes:
Control processes
When a command keeps running after theexec() call returns, you can check on it without blocking or wait for it to finish on your own terms. The Process handle provides methods for monitoring and control: