You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python SDKv2更新Azure机器学习计算实例的问题

解决Azure ML计算实例批量配置自动关机的问题

一、Python SDKv2 正确实现方法

你遇到的问题核心是直接复用ml_client.compute.get()返回的对象包含了ComputeInstance不支持的location属性,且未正确构造自动关机的配置结构。以下是修正后的代码:

代码示例

from azure.ai.ml import MLClient
from azure.ai.ml.entities import ComputeInstance, ComputeInstanceSchedule
from azure.identity import DefaultAzureCredential

# 初始化ML客户端
ml_client = MLClient(
    credential=DefaultAzureCredential(),
    subscription_id="你的订阅ID",
    resource_group_name="你的资源组名",
    workspace_name="你的AML工作区名"
)

# 遍历所有计算实例,筛选ComputeInstance类型
for compute in ml_client.compute.list():
    if compute.type == "computeinstance":
        # 重新构造ComputeInstance对象,仅保留必要属性
        ci_update = ComputeInstance(
            name=compute.name,
            idle_time_before_shutdown_minutes=30,  # 闲置30分钟关机
            schedule=ComputeInstanceSchedule(
                shutdown=ComputeInstanceSchedule.Shutdown(
                    time="20:00",  # 每日20点强制关机
                    time_zone="Asia/Shanghai"  # 根据你的时区调整
                )
            )
        )
        # 执行更新操作
        ml_client.begin_create_or_update(ci_update).result()
        print(f"已更新计算实例: {compute.name}")

关键说明

  • 不要直接复用ml_client.compute.get()返回的对象,避免带入location等不支持的属性,手动构造仅包含自动关机配置的ComputeInstance对象即可。
  • schedule参数需要嵌套ComputeInstanceSchedule.Shutdown结构,指定关机时间及时区,确保时区设置与你的实际需求匹配。

二、AZ CLI 替代方案(规避参数识别bug)

由于az ml compute update的idle参数存在已知bug,可直接调用Azure ARM API完成更新:

单实例更新命令

# 替换以下占位符为你的实际信息
SUBSCRIPTION_ID="你的订阅ID"
RESOURCE_GROUP="你的资源组名"
WORKSPACE_NAME="你的AML工作区名"
COMPUTE_NAME="目标计算实例名"

az rest --method patch \
  --uri "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.MachineLearningServices/workspaces/$WORKSPACE_NAME/computes/$COMPUTE_NAME?api-version=2024-04-01" \
  --body '{
    "properties": {
      "properties": {
        "idleTimeBeforeShutdown": "PT30M",
        "schedule": {
          "shutdown": {
            "time": "20:00",
            "timeZone": "Asia/Shanghai"
          }
        }
      }
    }
  }'

批量更新脚本

如果需要批量处理所有ComputeInstance,可结合shell脚本遍历:

# 替换以下占位符为你的实际信息
SUBSCRIPTION_ID="你的订阅ID"
RESOURCE_GROUP="你的资源组名"
WORKSPACE_NAME="你的AML工作区名"

# 获取所有ComputeInstance名称
COMPUTE_LIST=$(az ml compute list --workspace-name $WORKSPACE_NAME --resource-group $RESOURCE_GROUP --query "[?type=='computeinstance'].name" -o tsv)

# 逐个执行更新
for NAME in $COMPUTE_LIST; do
  az rest --method patch \
    --uri "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.MachineLearningServices/workspaces/$WORKSPACE_NAME/computes/$NAME?api-version=2024-04-01" \
    --body '{
      "properties": {
        "properties": {
          "idleTimeBeforeShutdown": "PT30M",
          "schedule": {
            "shutdown": {
              "time": "20:00",
              "timeZone": "Asia/Shanghai"
            }
          }
        }
      }
    }'
done

内容的提问来源于stack exchange,提问作者BeGreen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 07:16:23