Important
이 기능은 베타 버전으로 제공됩니다. 작업 공간에서 이 기능 미리보기를 활성화하려면 작업 공간 Previews 페이지에서 Databricks 샌드박스 설정에 선택하세요. Azure Databricks 미리 보기 관리를 참조하세요.
Databricks는 현재 이 기능 사용에 대해 요금을 부과하지 않습니다. 하지만 베타 기능의 비용과 가격은 변경될 수 있습니다.
Databricks SDK for Python에는 샌드박스를 생성하고 명령어 실행하며 수명 주기를 관리하는 샌드박스 클라이언트가 포함되어 있습니다. 이 페이지는 그 작전들을 하나하나 안내합니다. Databricks 샌드박스에 대한 개요는 Databricks Sandbox를 참조하세요.
SDK 설치
샌드박스 클라이언트를 포함한 Databricks SDK for Python을 설치하거나 업그레이드하세요:
pip install --upgrade databricks-sdk
WorkspaceClient 생성하기
모든 샌드박스 작업은 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로 샌드박스를 만드세요. 선택적으로 컴퓨트 사양을 설정할 수 있는데, 예를 들어 활동 없이 샌드박스가 얼마나 오래 실행되는지 제어하는 비활성 타임아웃(inactivity timeout)이 있습니다:
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)
샌드박스에서 명령을 실행하세요
샌드박스에서 명령을 실행하고 그 출력을 읽어보세요. 첫 번째 인자는 샌드박스 이름이고, 그 다음에 실행할 프로그램과 그 인수들이 있습니다. 이것은 셸을 거치지 않는 직접 exec이므로 셸 명령을 실행하려면 /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")