Important
此功能在 Beta 版中。 要在工作区中启用此功能预览,请选择在工作区预览页面启用 Databricks沙盒 设置。 请参阅 Manage Azure Databricks 预览版。
Databricks目前不对使用此功能收费。 不过,测试版功能的成本和定价可能会有所调整。
Databricks SDK for Python 包含一个沙箱客户端,用于创建沙盒、运行命令和管理其生命周期。 本页面详细介绍了这些操作。 有关Databricks沙盒的概述,请参见 Databricks沙盒。
安装 SDK
安装或升级包含沙盒客户端的 Python 版 Databricks SDK:
pip install --upgrade databricks-sdk
创建一个工作区客户端
所有沙箱操作都通过 WorkspaceClient 执行。 使用从您的环境、配置文件或显式指定的主机和令牌中解析出的凭证创建 WorkspaceClient:
from databricks.sdk import WorkspaceClient
# Resolve credentials from the environment or the default config profile:
w = WorkspaceClient()
# Or select a specific ~/.databrickscfg profile:
w = WorkspaceClient(profile="my-workspace")
# Or pass the host and token explicitly:
w = WorkspaceClient(host="https://<workspace>.cloud.databricks.com", token="dapi...")
有关配置和认证SDK的更多信息,请参见Databricks SDK for Python。
创建一个沙盒
用ID创建一个沙盒。 你可以根据需要配置计算规格,例如控制沙箱在无活动时保持运行多久的超时设置:
from google.protobuf.duration_pb2 import Duration
from databricks.sdk.service.sandbox import ComputeSpec, Sandbox, SandboxSpec
created = w.sandbox.create_sandbox(
sandbox=Sandbox(
spec=SandboxSpec(compute=ComputeSpec(inactivity_timeout=Duration(seconds=900))),
),
sandbox_id="my-sandbox",
)
print(created.name) # "sandboxes/my-sandbox"
获取并列出沙盒
按名称检索单个沙盒,或列出所有沙盒:
sandbox = w.sandbox.get_sandbox("sandboxes/my-sandbox")
print(sandbox.status.state) # SandboxState.SANDBOX_STATE_RUNNING
for sandbox in w.sandbox.list_sandboxes():
print(sandbox.name, sandbox.status.state if sandbox.status else None)
在沙盒中运行命令
在沙盒中运行命令并读取其输出。 第一个参数是沙盒名称,随后是要运行的程序及其参数。 这是直接执行,不经过 shell,因此如需运行 shell 命令,请调用 /bin/bash -c。 将任何环境变量传递给 envs:
from google.protobuf.duration_pb2 import Duration
resp = w.sandbox.execute_command_sync(
"sandboxes/my-sandbox",
"/bin/bash",
args=["-c", "echo hello world && whoami"],
envs={"MY_VAR": "value"},
execution_timeout=Duration(seconds=30),
)
print(resp.status) # ExecuteCommandStatus.EXECUTE_COMMAND_STATUS_COMPLETED
print(resp.exit_code) # 0
print(resp.stdout) # "hello world\nsandbox-agent\n"
print(resp.stderr)
print(resp.truncated) # True if the output was truncated
更新沙盒元数据
你可以修改沙盒的显示名称和闲置超时时间。 仅支持以下字段路径。 其他任何路径都会返回 INVALID_PARAMETER_VALUE:
display_namespec.compute.inactivity_timeout
from databricks.sdk.common.types.fieldmask import FieldMask
from databricks.sdk.service.sandbox import ComputeSpec, Sandbox, SandboxSpec
from google.protobuf.duration_pb2 import Duration
# Rename the sandbox
w.sandbox.update_sandbox(
name="sandboxes/my-sandbox",
sandbox=Sandbox(display_name="renamed sandbox"),
update_mask=FieldMask(["display_name"]),
)
# Extend the inactivity timeout
w.sandbox.update_sandbox(
name="sandboxes/my-sandbox",
sandbox=Sandbox(spec=SandboxSpec(compute=ComputeSpec(inactivity_timeout=Duration(seconds=1800)))),
update_mask=FieldMask(["spec.compute.inactivity_timeout"]),
)
停止并重启沙盒
停止沙箱以释放其计算资源,同时保留你的主目录:
w.sandbox.stop_sandbox("sandboxes/my-sandbox") # Moves to SANDBOX_STATE_STOPPED
重新启动沙盒以恢复工作:
w.sandbox.start_sandbox("sandboxes/my-sandbox") # Moves to SANDBOX_STATE_RUNNING
删除沙盒
完成后删除沙盒。 删除沙盒会移除其主目录,并停止对该沙盒的计费:
w.sandbox.delete_sandbox("sandboxes/my-sandbox")