在 While 循环 python 中,测试条件变量值未更新 - AWS - Boto3

In While loop python, the test condition variable value is not updating - AWS - Boto3

您好,我是一个非常新的 python 程序员,我遇到了配额查询代码的问题。

def get_metric_statistics(instance_args):
CMD  = (**querying the quota***)
print (CMD)
output = run_command_str(command=CMD, shell_command=True)
applied_quota_value = json.loads(output)
max_quota_metric = applied_quota_value ['Quota']['Value']
print ("max_quota:", max_quota_metric)

CMD1 = (***querying metric**)
print (CMD1)
output1 = run_command_str(command=CMD1, shell_command=True)
print ("output:", output1)
utilized_quota_value = json.loads(output1)
used_quota_metric = utilized_quota_value ['Datapoints'][0]['Maximum']
print ("used:", used_quota_metric)
available_quota = max_quota_metric - used_quota_metric
instance_args["no_of_vcpu"] = some value

while available_quota < instance_args["no_of_vcpu"]:
    print ("Waiting for vcpu to be available")
    time.sleep(30)
else:
    print ("available quota is greater than requested vcpu, continues to launch the instance")
aws_metric_data = get_metric_statistics(instance_args)
print(aws_metric_data)

所以我在这里尝试检查条件并打印后续语句,但是当条件中具有值的变量进入循环时,它会检查条件是否为真,即使在变量之后,真实条件也会循环已更新为不同的值。条件中的变量基本上没有得到更新。不确定如何获取在循环内进行更新的那段代码。

在您的代码中:

while available_quota < instance_args["no_of_vcpu"]:
    print ("Waiting for vcpu to be available")
    time.sleep(30)
else:
    print ("available quota is greater than requested vcpu, continues to launch the instance")

while 循环使用两个值进行比较。但是由于值没有在循环内更新,while 循环将永远运行下去。您还需要使用最初用于计算值的代码不断检查循环内的值。

像这样:

while available_quota < instance_args["no_of_vcpu"]:
    print ("Waiting for vcpu to be available")
    time.sleep(30)
    output1 = run_command_str(command=CMD1, shell_command=True)
    print ("output:", output1)
    utilized_quota_value = json.loads(output1)
    used_quota_metric = utilized_quota_value ['Datapoints'][0]['Maximum']
    print ("used:", used_quota_metric)
    available_quota = max_quota_metric - used_quota_metric
    instance_args["no_of_vcpu"] = some value
else:
    print ("available quota is greater than requested vcpu, continues to launch the instance")

虽然附加部分是一个独立的工作组件,但我可能会将其放入一个函数中。

 def getStatus():
     output1 = run_command_str(command=CMD1, shell_command=True)
     print ("output:", output1)
     utilized_quota_value = json.loads(output1)
     used_quota_metric = utilized_quota_value ['Datapoints'][0]['Maximum']
     print ("used:", used_quota_metric)
     available_quota = max_quota_metric - used_quota_metric
     instance_args["no_of_vcpu"] = some value

     return available_quota, instance_args["no_of_vcpu"]

然后:

while available_quota < instance_args["no_of_vcpu"]:
    print ("Waiting for vcpu to be available")
    time.sleep(30)
    available_quota,instance_args["no_of_vcpu"]=getStatus()
else:
    print ("available quota is greater than requested vcpu, continues to launch the instance")

while 循环中未更新变量。我已将您的计算移至一个单独的函数中。当 while 循环在睡眠后运行时,将重新计算可用配额

def calc_available_quota():
    CMD  = (**querying the quota***)
    print (CMD)
    output = run_command_str(command=CMD, shell_command=True)
    applied_quota_value = json.loads(output)
    max_quota_metric = applied_quota_value ['Quota']['Value']
    print ("max_quota:", max_quota_metric)

    CMD1 = (***querying metric**)
    print (CMD1)
    output1 = run_command_str(command=CMD1, shell_command=True)
    print ("output:", output1)
    utilized_quota_value = json.loads(output1)
    used_quota_metric = utilized_quota_value ['Datapoints'][0]['Maximum']
    print ("used:", used_quota_metric)
    return max_quota_metric - used_quota_metric


def get_metric_statistics(instance_args):
    instance_args["no_of_vcpu"] = some_value
    available_quota = calc_available_quota()

    while available_quota < instance_args["no_of_vcpu"]:
        print ("Waiting for vcpu to be available")
        time.sleep(30)
        available_quota = calc_available_quota()
    else:
        print ("available quota is greater than requested vcpu, continues to launch the instance")
    aws_metric_data = get_metric_statistics(instance_args)
    print(aws_metric_data)