简体   繁体   English

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

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

Hi I'm a very new python programmer and I have encountered a problem with quota query code.嗨,我是一个非常新的 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)

so here im trying to check the condition and print the subsequent statements, But when the variable with the value in the condition enters the loop, it checks the condition is true and the true condition is looping over even after the variable has been updated by a different value.所以这里我试图检查条件并打印后续语句,但是当条件中的值的变量进入循环时,它会检查条件是否为真,即使在变量已被更新后,真条件仍在循环不同的价值。 The variable in the condition is basically not getting updated.条件中的变量基本上没有更新。 Not sure how to get that piece of code which does the update inside the loop.不知道如何获取在循环内进行更新的那段代码。

In your code:在您的代码中:

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")

The while loop is entered with two values, which are compared.使用两个值进入 while 循环,这两个值进行比较。 But since the values are not being updated within the loop, the while loop will go around forever.但是由于循环内的值没有被更新,while循环将永远存在go。 You need to continually check the values within the loop as well, using the code which you used to calculate the values in the first place.您还需要使用最初用于计算值的代码不断检查循环中的值。

Something like this:像这样的东西:

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")

Although since the additional part is a standalone working component, I might put it into a function.虽然由于附加部分是一个独立的工作组件,但我可能会将其放入 function 中。

 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"]

Then:然后:

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")

The variable is not being updated in the while loop.该变量未在 while 循环中更新。 I've moved your calculations into a seperate function.我已将您的计算转移到单独的 function 中。 When the while loop runs after the sleep the available quota will be recalculated当 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM