簡體   English   中英

如何獲取 AWS boto python EC2 實例中的 vCPU 數量?

[英]How to get number of vCPU in AWS boto python EC2 instance?

有沒有辦法通過 boto python api 獲取 EC2 實例中的 vCPU(核心)數量?

到目前為止我的代碼是:

在 boto3 中找到它:

import boto3     
ec2 = boto3.resource('ec2', region_name=REGION_NAME)
instance = ec2.Instance(id)
instance.cpu_options.get(u'CoreCount','1')

這返回了我的物理內核數,但是通過 htop 我可以看到我的 VCPU 是 VCPU 的兩倍,它總是這樣嗎? 也許它與 ThreadsPerCore 的數量有關?

我是這樣做的,需要最新版本的boto3:

import boto3
# You may have to update boto3 to get this to work, describe_instance_types is quite new:
# pip install boto3 --upgrade
ec2 = boto3.client('ec2')

reservations = ec2.describe_instances()['Reservations']
vcpus = 0
for reservation in reservations:
    for instance in reservation['Instances']:
        instance_id = instance['InstanceId']
        instance_type_name = instance['InstanceType']
        instance_type = ec2.describe_instance_types(InstanceTypes=[instance_type_name])
        current_vcpu_count = instance_type['InstanceTypes'][0]['VCpuInfo']['DefaultVCpus']
        print(f'{instance_id} - {instance_type_name} - {current_vcpu_count}')
        vcpus += current_vcpu_count

print(vcpus)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM