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

如何使用Python Azure SDK查询Azure Spot VM vCPU配额?

Azure配额自动化无法获取Spot vCPU使用量问题解决

问题场景

使用计算类Python SDK做Azure配额检查自动化,现有代码可正常拉取VM、常规vCPU、规模集等资源的使用量与分配配额,但始终无法获取Spot vCPU的相关数据,使用官方CLI命令az vm list-usage同样查不到Spot vCPU使用情况。

现有Python测试代码

from os import environ

from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.monitor import MonitorClient
from azure.mgmt.resource import ResourceManagementClient
subscription_id = environ["AZURE_SUBSCRIPTION_ID"]
credentials     #add token credentials 
y=compute_client.usage.list('centralindia')
for i in y:
    print(i.name.value,i.limit,i.current_value)

现有CLI测试命令

az vm list-usage -l westus

根因说明

常规计算Usage接口默认绑定的旧版API(2021-03-01之前版本)未纳入Spot vCPU配额维度,Spot vCPU属于独立配额项,不会在旧接口返回结果中展示。

解决方案

1. CLI侧修复

首先将本地Azure CLI升级到最新版本,旧版CLI默认调用旧版Compute API,无法拉取Spot数据:

az upgrade

升级后执行查询命令即可看到Spot相关配额项,可直接过滤Spot条目快速查看:

# 查询westus区域所有计算配额,过滤Spot vCPU相关结果
az vm list-usage -l westus --query "[?contains(name.value, 'Spot')]"

2. Python SDK侧修复

首先升级依赖包到满足版本要求:

pip install --upgrade azure-mgmt-compute>=20.0.0 azure-identity

修正后的可运行代码如下,初始化客户端时指定支持Spot配额的API版本即可拉取全量数据:

from os import environ
from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient

subscription_id = environ["AZURE_SUBSCRIPTION_ID"]
credential = DefaultAzureCredential()
# 初始化计算客户端,指定2021-03-01之后的稳定API版本
compute_client = ComputeManagementClient(
    credential=credential,
    subscription_id=subscription_id,
    api_version="2023-07-01"
)

target_region = "centralindia"
usage_list = compute_client.usage.list(location=target_region)
for usage in usage_list:
    # 全量打印可看到所有资源配额,其中standardSpotvCPUs对应Spot vCPU配额
    print(f"资源类型: {usage.name.value}, 总配额: {usage.limit}, 当前使用量: {usage.current_value}")

注意事项

  • 返回结果中standardSpotvCPUs为常规Spot vCPU配额项,lowPriorityvCPUs为低优先级VM配额项,规模集的Spot实例使用量会自动汇总到对应条目的当前使用值中,无需额外调用规模集接口统计
  • 如果升级后依然查询不到Spot配额项,先确认当前订阅在对应区域是否已开通Spot VM使用权限,未开通权限的订阅不会返回该配额维度
  • 不要使用Monitor接口单独查询Spot用量,Monitor的指标数据延迟在15分钟到1小时不等,直接调用Compute Usage接口返回的是实时配额数据

内容的提问来源于stack exchange,提问作者akash S E

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.03 00:18:20