This guide shows you how to run commands in sandboxes using the 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. The exec() 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():
Use streaming when you need to:
- Monitor long-running processes.
- Process output as it arrives.
- Implement progress indicators.
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 with stdin=True:
StreamWriter methods
When stdin=True, process.stdin is a StreamWriter with three methods:
write(data: bytes): Write raw bytes. Returns OperationRef[None].
writeline(text: str): Write text with a trailing newline (encodes to UTF-8). Returns OperationRef[None].
close(): Signal EOF. The system completes pending writes first. Returns OperationRef[None].
When 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 (like sort) 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 with cwd:
The path must be absolute.
Set a timeout
To stop a command that might freeze or run longer than you expect, set a timeout with timeout_seconds:
Handle errors with check
The check 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
Raises SandboxExecutionError 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
Use cwsandbox.wait() to wait for a subset of processes:
Control processes
When a command keeps running after the exec() 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:
Last modified on May 29, 2026