Sandboxes
zenml.sandboxes
Sandbox stack components for isolated code execution.
Attributes
LOCAL_SANDBOX_FLAVOR = 'local'
module-attribute
__all__ = ['LOCAL_SANDBOX_FLAVOR', 'BaseSandbox', 'BaseSandboxConfig', 'BaseSandboxFlavor', 'BaseSandboxSettings', 'SandboxSnapshot', 'LocalSandbox', 'LocalSandboxConfig', 'LocalSandboxFlavor', 'LocalSandboxSettings', 'SandboxExecError', 'SandboxOutput', 'SandboxProcess', 'SandboxSession', 'SandboxSessionClosedError']
module-attribute
Classes
BaseSandbox(name: str, id: UUID, config: StackComponentConfig, flavor: str, type: StackComponentType, user: Optional[UUID], created: datetime, updated: datetime, environment: Optional[Dict[str, str]] = None, secrets: Optional[List[UUID]] = None, labels: Optional[Dict[str, Any]] = None, connector_requirements: Optional[ServiceConnectorRequirements] = None, connector: Optional[UUID] = None, connector_resource_id: Optional[str] = None, *args: Any, **kwargs: Any)
Bases: StackComponent, ABC
Base class for all ZenML sandbox components.
Source code in src/zenml/stack/stack_component.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
Attributes
config: BaseSandboxConfig
property
The sandbox component configuration.
Returns:
| Type | Description |
|---|---|
BaseSandboxConfig
|
The sandbox component configuration. |
settings_class: Optional[Type[BaseSettings]]
property
The sandbox settings class.
Returns:
| Type | Description |
|---|---|
Optional[Type[BaseSettings]]
|
The sandbox settings class. |
Methods:
attach(session_id: str) -> SandboxSession
Attach to a running sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The ID of the running sandbox session. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the sandbox does not support attaching to a running sandbox session. |
Returns:
| Type | Description |
|---|---|
SandboxSession
|
Sandbox session. |
Source code in src/zenml/sandboxes/base.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
create_session(settings: Optional[BaseSandboxSettings] = None) -> SandboxSession
abstractmethod
Create a fresh sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Optional[BaseSandboxSettings]
|
The sandbox settings. |
None
|
Returns:
| Type | Description |
|---|---|
SandboxSession
|
A new sandbox session. |
Source code in src/zenml/sandboxes/base.py
75 76 77 78 79 80 81 82 83 84 85 86 | |
resolve_settings(override: Optional[BaseSandboxSettings] = None) -> BaseSandboxSettings
Resolve the sandbox settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
override
|
Optional[BaseSandboxSettings]
|
Per-call settings override. If provided these will override values in the component settings/config. |
None
|
Returns:
| Type | Description |
|---|---|
BaseSandboxSettings
|
The resolved settings. |
Source code in src/zenml/sandboxes/base.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
restore(snapshot: SandboxSnapshot) -> SandboxSession
Restore a sandbox session from a snapshot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshot
|
SandboxSnapshot
|
The snapshot to restore from. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the sandbox does not support restoring from a snapshot. |
Returns:
| Type | Description |
|---|---|
SandboxSession
|
Sandbox session. |
Source code in src/zenml/sandboxes/base.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
BaseSandboxConfig(warn_about_plain_text_secrets: bool = False, **kwargs: Any)
Bases: StackComponentConfig
Sandbox configuration.
Source code in src/zenml/stack/stack_component.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
Attributes
is_local: bool
property
Whether this sandbox component is running locally.
Returns:
| Type | Description |
|---|---|
bool
|
Whether this sandbox component is running locally. |
BaseSandboxFlavor
Bases: Flavor
Base sandbox flavor.
Attributes
config_class: Type[BaseSandboxConfig]
property
implementation_class: Type[BaseSandbox]
abstractmethod
property
type: StackComponentType
property
BaseSandboxSettings(warn_about_plain_text_secrets: bool = False, **kwargs: Any)
Bases: BaseSettings
Sandbox settings.
Source code in src/zenml/config/secret_reference_mixin.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
LocalSandbox(name: str, id: UUID, config: StackComponentConfig, flavor: str, type: StackComponentType, user: Optional[UUID], created: datetime, updated: datetime, environment: Optional[Dict[str, str]] = None, secrets: Optional[List[UUID]] = None, labels: Optional[Dict[str, Any]] = None, connector_requirements: Optional[ServiceConnectorRequirements] = None, connector: Optional[UUID] = None, connector_resource_id: Optional[str] = None, *args: Any, **kwargs: Any)
Bases: BaseSandbox
Local subprocess-based Sandbox.
Warning: This class does NOT provide any isolation and is not suitable for running untrusted code.
Source code in src/zenml/stack/stack_component.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
Attributes
config: LocalSandboxConfig
property
Local sandbox configuration.
Returns:
| Type | Description |
|---|---|
LocalSandboxConfig
|
The local sandbox configuration. |
settings_class: Optional[Type[BaseSandboxSettings]]
property
Methods:
create_session(settings: Optional[BaseSandboxSettings] = None) -> SandboxSession
Create a local sandbox session in a clean working directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Optional[BaseSandboxSettings]
|
Optional settings overrides. |
None
|
Returns:
| Type | Description |
|---|---|
SandboxSession
|
A local sandbox session. |
Source code in src/zenml/sandboxes/local_sandbox.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
LocalSandboxConfig(warn_about_plain_text_secrets: bool = False, **kwargs: Any)
Bases: BaseSandboxConfig, LocalSandboxSettings
Local sandbox configuration.
Source code in src/zenml/stack/stack_component.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
LocalSandboxFlavor
Bases: BaseSandboxFlavor
Local subprocess sandbox flavor.
Attributes
config_class: Type[LocalSandboxConfig]
property
docs_url: Optional[str]
property
Docs URL.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The docs URL. |
implementation_class: Type[LocalSandbox]
property
logo_url: str
property
Dashboard logo URL.
Returns:
| Type | Description |
|---|---|
str
|
The flavor logo URL. |
name: str
property
Flavor name.
Returns:
| Type | Description |
|---|---|
str
|
The flavor name. |
sdk_docs_url: Optional[str]
property
SDK docs URL.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The flavor SDK docs URL. |
LocalSandboxSettings(warn_about_plain_text_secrets: bool = False, **kwargs: Any)
Bases: BaseSandboxSettings
Local sandbox settings.
Source code in src/zenml/config/secret_reference_mixin.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
SandboxExecError
Bases: RuntimeError
Raised when a sandbox command fails to launch.
SandboxOutput(stdout: str, stderr: str, exit_code: int, stdout_truncated: bool = False, stderr_truncated: bool = False)
dataclass
Result of fully consuming a sandbox process.
SandboxProcess(session: SandboxSession, started_at: float)
Bases: ABC
Handle to a command being executed inside a sandbox session.
Initialize the sandbox process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
SandboxSession
|
The owning session. |
required |
started_at
|
float
|
The wall-clock time the process started. |
required |
Source code in src/zenml/sandboxes/process.py
49 50 51 52 53 54 55 56 57 58 | |
Attributes
exit_code: Optional[int]
abstractmethod
property
Exit code, or None if the command is still running.
Methods:
collect(*, max_chars: int = _DEFAULT_COLLECT_CHAR_COUNT) -> SandboxOutput
Wait for the process to exit and return the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_chars
|
int
|
Maximum number of characters to collect per stream. |
_DEFAULT_COLLECT_CHAR_COUNT
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the process output was already collected. The streams are drained by the first call, so a second call would fabricate an empty result instead of the real output. |
drain_exception
|
Re-raised from a failed stdout or stderr drain. |
Returns:
| Type | Description |
|---|---|
SandboxOutput
|
The output of the process. |
Source code in src/zenml/sandboxes/process.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
kill() -> None
abstractmethod
Terminates the process.
Source code in src/zenml/sandboxes/process.py
79 80 81 | |
stderr() -> Iterator[str]
abstractmethod
Yields stderr lines.
Source code in src/zenml/sandboxes/process.py
64 65 66 | |
stdout() -> Iterator[str]
abstractmethod
Yields stdout lines.
Source code in src/zenml/sandboxes/process.py
60 61 62 | |
wait(timeout: Optional[float] = None) -> int
abstractmethod
Blocks until the process exits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
Optional[float]
|
Timeout in seconds to wait. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The exit code. |
Source code in src/zenml/sandboxes/process.py
68 69 70 71 72 73 74 75 76 77 | |
SandboxSession(*, id: str, parent: BaseSandbox)
Bases: ABC
Sandbox session.
Initialize the sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Session identifier. |
required |
parent
|
BaseSandbox
|
The sandbox component that created this session. |
required |
Source code in src/zenml/sandboxes/session.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
Attributes
closed: bool
property
Whether this session handle has been closed.
Returns:
| Type | Description |
|---|---|
bool
|
|
Methods:
aexec(command: Union[str, List[str]], *, cwd: Optional[str] = None, env: Optional[Dict[str, str]] = None) -> SandboxProcess
async
Async execute a command in the sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Union[str, List[str]]
|
The command to execute. |
required |
cwd
|
Optional[str]
|
Optional working directory override. |
None
|
env
|
Optional[Dict[str, str]]
|
Optional environment variables to set in the environment executing the command. |
None
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the sandbox does not support async exec. |
Returns:
| Type | Description |
|---|---|
SandboxProcess
|
Process handle. |
Source code in src/zenml/sandboxes/session.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
close() -> None
Close the sandbox session handle. Terminal and idempotent.
After closing, the session handle rejects further use (exec,
snapshots, file transfer) — re-attach to the sandbox for a fresh
handle. For remote sandboxes, closing does not terminate the
sandbox on the provider: it keeps running until its TTL expires
or destroy() is called.
Source code in src/zenml/sandboxes/session.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
create_snapshot() -> SandboxSnapshot
Create a snapshot of the sandbox session.
Returns:
| Type | Description |
|---|---|
SandboxSnapshot
|
A sandbox snapshot. |
Source code in src/zenml/sandboxes/session.py
159 160 161 162 163 164 165 166 | |
destroy() -> None
Destroy the sandbox session. Terminal like close().
Terminates the sandbox on the provider and then closes this
handle, so after a successful call it is no longer possible to
attach to the session. Flavor failures (including
NotImplementedError from flavors without destroy support)
propagate and leave the handle open for retry, because the
flavor hook runs before close(). Callable on an
already-closed handle: close() is idempotent.
Source code in src/zenml/sandboxes/session.py
256 257 258 259 260 261 262 263 264 265 266 267 268 | |
download_file(remote_path: str, local_path: str) -> None
Download a file from the sandbox to the local filesystem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_path
|
str
|
Source path in the sandbox. |
required |
local_path
|
str
|
Destination path on the caller's filesystem. |
required |
Source code in src/zenml/sandboxes/session.py
206 207 208 209 210 211 212 213 214 | |
exec(command: Union[str, List[str]], *, cwd: Optional[str] = None, env: Optional[Dict[str, str]] = None) -> SandboxProcess
Execute a command in the sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Union[str, List[str]]
|
The command to execute. |
required |
cwd
|
Optional[str]
|
Optional working directory override. |
None
|
env
|
Optional[Dict[str, str]]
|
Optional environment variables to set in the environment executing the command. |
None
|
Returns:
| Type | Description |
|---|---|
SandboxProcess
|
Process handle. |
Source code in src/zenml/sandboxes/session.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
upload_file(local_path: str, remote_path: str) -> None
Upload a file to the sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path
|
str
|
Source path on the caller's filesystem. |
required |
remote_path
|
str
|
Destination path in the sandbox. |
required |
Source code in src/zenml/sandboxes/session.py
182 183 184 185 186 187 188 189 190 | |
SandboxSessionClosedError
Bases: RuntimeError
Raised when a closed sandbox session is used.
SandboxSnapshot
Bases: BaseModel
Sandbox snapshot.
Modules
base
Base sandbox flavor and component.
Classes
BaseSandbox(name: str, id: UUID, config: StackComponentConfig, flavor: str, type: StackComponentType, user: Optional[UUID], created: datetime, updated: datetime, environment: Optional[Dict[str, str]] = None, secrets: Optional[List[UUID]] = None, labels: Optional[Dict[str, Any]] = None, connector_requirements: Optional[ServiceConnectorRequirements] = None, connector: Optional[UUID] = None, connector_resource_id: Optional[str] = None, *args: Any, **kwargs: Any)
Bases: StackComponent, ABC
Base class for all ZenML sandbox components.
Source code in src/zenml/stack/stack_component.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
config: BaseSandboxConfig
property
The sandbox component configuration.
Returns:
| Type | Description |
|---|---|
BaseSandboxConfig
|
The sandbox component configuration. |
settings_class: Optional[Type[BaseSettings]]
property
The sandbox settings class.
Returns:
| Type | Description |
|---|---|
Optional[Type[BaseSettings]]
|
The sandbox settings class. |
attach(session_id: str) -> SandboxSession
Attach to a running sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
The ID of the running sandbox session. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the sandbox does not support attaching to a running sandbox session. |
Returns:
| Type | Description |
|---|---|
SandboxSession
|
Sandbox session. |
Source code in src/zenml/sandboxes/base.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
create_session(settings: Optional[BaseSandboxSettings] = None) -> SandboxSession
abstractmethod
Create a fresh sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Optional[BaseSandboxSettings]
|
The sandbox settings. |
None
|
Returns:
| Type | Description |
|---|---|
SandboxSession
|
A new sandbox session. |
Source code in src/zenml/sandboxes/base.py
75 76 77 78 79 80 81 82 83 84 85 86 | |
resolve_settings(override: Optional[BaseSandboxSettings] = None) -> BaseSandboxSettings
Resolve the sandbox settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
override
|
Optional[BaseSandboxSettings]
|
Per-call settings override. If provided these will override values in the component settings/config. |
None
|
Returns:
| Type | Description |
|---|---|
BaseSandboxSettings
|
The resolved settings. |
Source code in src/zenml/sandboxes/base.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
restore(snapshot: SandboxSnapshot) -> SandboxSession
Restore a sandbox session from a snapshot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snapshot
|
SandboxSnapshot
|
The snapshot to restore from. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the sandbox does not support restoring from a snapshot. |
Returns:
| Type | Description |
|---|---|
SandboxSession
|
Sandbox session. |
Source code in src/zenml/sandboxes/base.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
BaseSandboxConfig(warn_about_plain_text_secrets: bool = False, **kwargs: Any)
Bases: StackComponentConfig
Sandbox configuration.
Source code in src/zenml/stack/stack_component.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
is_local: bool
property
Whether this sandbox component is running locally.
Returns:
| Type | Description |
|---|---|
bool
|
Whether this sandbox component is running locally. |
BaseSandboxFlavor
Bases: Flavor
Base sandbox flavor.
config_class: Type[BaseSandboxConfig]
property
implementation_class: Type[BaseSandbox]
abstractmethod
property
type: StackComponentType
property
BaseSandboxSettings(warn_about_plain_text_secrets: bool = False, **kwargs: Any)
Bases: BaseSettings
Sandbox settings.
Source code in src/zenml/config/secret_reference_mixin.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
Functions:
local_sandbox
Local sandbox flavor.
Classes
LocalSandbox(name: str, id: UUID, config: StackComponentConfig, flavor: str, type: StackComponentType, user: Optional[UUID], created: datetime, updated: datetime, environment: Optional[Dict[str, str]] = None, secrets: Optional[List[UUID]] = None, labels: Optional[Dict[str, Any]] = None, connector_requirements: Optional[ServiceConnectorRequirements] = None, connector: Optional[UUID] = None, connector_resource_id: Optional[str] = None, *args: Any, **kwargs: Any)
Bases: BaseSandbox
Local subprocess-based Sandbox.
Warning: This class does NOT provide any isolation and is not suitable for running untrusted code.
Source code in src/zenml/stack/stack_component.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |
config: LocalSandboxConfig
property
Local sandbox configuration.
Returns:
| Type | Description |
|---|---|
LocalSandboxConfig
|
The local sandbox configuration. |
settings_class: Optional[Type[BaseSandboxSettings]]
property
create_session(settings: Optional[BaseSandboxSettings] = None) -> SandboxSession
Create a local sandbox session in a clean working directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Optional[BaseSandboxSettings]
|
Optional settings overrides. |
None
|
Returns:
| Type | Description |
|---|---|
SandboxSession
|
A local sandbox session. |
Source code in src/zenml/sandboxes/local_sandbox.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
LocalSandboxConfig(warn_about_plain_text_secrets: bool = False, **kwargs: Any)
Bases: BaseSandboxConfig, LocalSandboxSettings
Local sandbox configuration.
Source code in src/zenml/stack/stack_component.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
LocalSandboxFlavor
Bases: BaseSandboxFlavor
Local subprocess sandbox flavor.
config_class: Type[LocalSandboxConfig]
property
docs_url: Optional[str]
property
Docs URL.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The docs URL. |
implementation_class: Type[LocalSandbox]
property
logo_url: str
property
Dashboard logo URL.
Returns:
| Type | Description |
|---|---|
str
|
The flavor logo URL. |
name: str
property
Flavor name.
Returns:
| Type | Description |
|---|---|
str
|
The flavor name. |
sdk_docs_url: Optional[str]
property
SDK docs URL.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The flavor SDK docs URL. |
LocalSandboxProcess(process: subprocess.Popen[str], session: LocalSandboxSession, started_at: float)
Bases: SandboxProcess
Local sandbox process wrapping a subprocess.
Initialize the local sandbox process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process
|
Popen[str]
|
The subprocess handle. |
required |
session
|
LocalSandboxSession
|
The owning session. |
required |
started_at
|
float
|
The wall-clock time the launch began. |
required |
Source code in src/zenml/sandboxes/local_sandbox.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
exit_code: Optional[int]
property
Exit code, or None if the subprocess is still running.
Returns:
| Type | Description |
|---|---|
Optional[int]
|
The exit code or |
kill() -> None
Kill the subprocess by sending SIGKILL.
Source code in src/zenml/sandboxes/local_sandbox.py
152 153 154 155 156 157 158 159 160 161 162 163 164 | |
stderr() -> Iterator[str]
Stderr line iterator.
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
Stderr line iterator. |
Source code in src/zenml/sandboxes/local_sandbox.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
stdout() -> Iterator[str]
Stdout line iterator.
Returns:
| Type | Description |
|---|---|
Iterator[str]
|
Stdout line iterator. |
Source code in src/zenml/sandboxes/local_sandbox.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
wait(timeout: Optional[float] = None) -> int
Block until the subprocess exits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
Optional[float]
|
Timeout in seconds to wait. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The exit code. |
Source code in src/zenml/sandboxes/local_sandbox.py
141 142 143 144 145 146 147 148 149 150 | |
LocalSandboxSession(workdir: str, env: Dict[str, str], *, parent: BaseSandbox)
Bases: SandboxSession
Local sandbox session.
Initialize the local sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workdir
|
str
|
Absolute path to the working directory for this session. This directory is cleaned up when the session is closed. |
required |
env
|
Dict[str, str]
|
Environment variables to set for this session. |
required |
parent
|
BaseSandbox
|
The sandbox component that created this session. |
required |
Source code in src/zenml/sandboxes/local_sandbox.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
LocalSandboxSettings(warn_about_plain_text_secrets: bool = False, **kwargs: Any)
Bases: BaseSandboxSettings
Local sandbox settings.
Source code in src/zenml/config/secret_reference_mixin.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
Functions:
process
Sandbox process.
Classes
SandboxExecError
Bases: RuntimeError
Raised when a sandbox command fails to launch.
SandboxOutput(stdout: str, stderr: str, exit_code: int, stdout_truncated: bool = False, stderr_truncated: bool = False)
dataclass
Result of fully consuming a sandbox process.
SandboxProcess(session: SandboxSession, started_at: float)
Bases: ABC
Handle to a command being executed inside a sandbox session.
Initialize the sandbox process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
SandboxSession
|
The owning session. |
required |
started_at
|
float
|
The wall-clock time the process started. |
required |
Source code in src/zenml/sandboxes/process.py
49 50 51 52 53 54 55 56 57 58 | |
exit_code: Optional[int]
abstractmethod
property
Exit code, or None if the command is still running.
collect(*, max_chars: int = _DEFAULT_COLLECT_CHAR_COUNT) -> SandboxOutput
Wait for the process to exit and return the output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_chars
|
int
|
Maximum number of characters to collect per stream. |
_DEFAULT_COLLECT_CHAR_COUNT
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the process output was already collected. The streams are drained by the first call, so a second call would fabricate an empty result instead of the real output. |
drain_exception
|
Re-raised from a failed stdout or stderr drain. |
Returns:
| Type | Description |
|---|---|
SandboxOutput
|
The output of the process. |
Source code in src/zenml/sandboxes/process.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
kill() -> None
abstractmethod
Terminates the process.
Source code in src/zenml/sandboxes/process.py
79 80 81 | |
stderr() -> Iterator[str]
abstractmethod
Yields stderr lines.
Source code in src/zenml/sandboxes/process.py
64 65 66 | |
stdout() -> Iterator[str]
abstractmethod
Yields stdout lines.
Source code in src/zenml/sandboxes/process.py
60 61 62 | |
wait(timeout: Optional[float] = None) -> int
abstractmethod
Blocks until the process exits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
Optional[float]
|
Timeout in seconds to wait. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The exit code. |
Source code in src/zenml/sandboxes/process.py
68 69 70 71 72 73 74 75 76 77 | |
Functions:
session
Sandbox session.
Classes
SandboxSession(*, id: str, parent: BaseSandbox)
Bases: ABC
Sandbox session.
Initialize the sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
Session identifier. |
required |
parent
|
BaseSandbox
|
The sandbox component that created this session. |
required |
Source code in src/zenml/sandboxes/session.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
closed: bool
property
Whether this session handle has been closed.
Returns:
| Type | Description |
|---|---|
bool
|
|
aexec(command: Union[str, List[str]], *, cwd: Optional[str] = None, env: Optional[Dict[str, str]] = None) -> SandboxProcess
async
Async execute a command in the sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Union[str, List[str]]
|
The command to execute. |
required |
cwd
|
Optional[str]
|
Optional working directory override. |
None
|
env
|
Optional[Dict[str, str]]
|
Optional environment variables to set in the environment executing the command. |
None
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the sandbox does not support async exec. |
Returns:
| Type | Description |
|---|---|
SandboxProcess
|
Process handle. |
Source code in src/zenml/sandboxes/session.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
close() -> None
Close the sandbox session handle. Terminal and idempotent.
After closing, the session handle rejects further use (exec,
snapshots, file transfer) — re-attach to the sandbox for a fresh
handle. For remote sandboxes, closing does not terminate the
sandbox on the provider: it keeps running until its TTL expires
or destroy() is called.
Source code in src/zenml/sandboxes/session.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
create_snapshot() -> SandboxSnapshot
Create a snapshot of the sandbox session.
Returns:
| Type | Description |
|---|---|
SandboxSnapshot
|
A sandbox snapshot. |
Source code in src/zenml/sandboxes/session.py
159 160 161 162 163 164 165 166 | |
destroy() -> None
Destroy the sandbox session. Terminal like close().
Terminates the sandbox on the provider and then closes this
handle, so after a successful call it is no longer possible to
attach to the session. Flavor failures (including
NotImplementedError from flavors without destroy support)
propagate and leave the handle open for retry, because the
flavor hook runs before close(). Callable on an
already-closed handle: close() is idempotent.
Source code in src/zenml/sandboxes/session.py
256 257 258 259 260 261 262 263 264 265 266 267 268 | |
download_file(remote_path: str, local_path: str) -> None
Download a file from the sandbox to the local filesystem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_path
|
str
|
Source path in the sandbox. |
required |
local_path
|
str
|
Destination path on the caller's filesystem. |
required |
Source code in src/zenml/sandboxes/session.py
206 207 208 209 210 211 212 213 214 | |
exec(command: Union[str, List[str]], *, cwd: Optional[str] = None, env: Optional[Dict[str, str]] = None) -> SandboxProcess
Execute a command in the sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
Union[str, List[str]]
|
The command to execute. |
required |
cwd
|
Optional[str]
|
Optional working directory override. |
None
|
env
|
Optional[Dict[str, str]]
|
Optional environment variables to set in the environment executing the command. |
None
|
Returns:
| Type | Description |
|---|---|
SandboxProcess
|
Process handle. |
Source code in src/zenml/sandboxes/session.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
upload_file(local_path: str, remote_path: str) -> None
Upload a file to the sandbox session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_path
|
str
|
Source path on the caller's filesystem. |
required |
remote_path
|
str
|
Destination path in the sandbox. |
required |
Source code in src/zenml/sandboxes/session.py
182 183 184 185 186 187 188 189 190 | |
SandboxSessionClosedError
Bases: RuntimeError
Raised when a closed sandbox session is used.
Functions:
snapshot
Sandbox snapshot.
Classes
SandboxSnapshot
Bases: BaseModel
Sandbox snapshot.