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

Time:01-08

Hi I'm a very new python programmer and I have encountered a problem with quota query code.

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.

CodePudding user response:

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. But since the values are not being updated within the loop, the while loop will go around forever. 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.

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

CodePudding user response:

The variable is not being updated in the while loop. I've moved your calculations into a seperate function. When the while loop runs after the sleep the available quota will be recalculated

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)
  •  Tags:  
  • Related