Why does my while loop in an if/else statement run infinitely?

Question:

I am geting data from an api every 15mins and I am trying to implement the following logic:

More updates = true & items array with data • There are changes/updates to be processed • The number of items sent in a single batch is limited to 100. There are more than 100 updates to receive so the more updates flag is true • process the received changes and immediately make another request for the next batch of (up to) 100 items.
More updates = false & items array with data • There are changes/updates to be processed .The number of items sent is 100 or fewer. • process the received changes, no need to make another request until your next planned scheduled request.
More updates = false & items array no data • There are no changes/updates to process. • no need to make another request until your next planned scheduled request.

I am using a while loop in an if statement and it runs endlessly. It also inserts the same data over and over instead of continuing with the next updates that need to be processed.

def process():
   
   data=get_data()
   more_updates=data["Update"]["MoreUpdates"]
   items=data["Update"]["Items"] 
   now=datetime.now()     
   
   try:
        get_data()
        if more_updates is True:
          while more_updates is True:
            insert_data()
            print(f"data inserted at {now}, items_updated: {len(items)}, more_updates: {more_updates}")
           
            send_teams_message("data successfully inserted in DB")  
            get_data()
        else:
            if len(items)>0 and len(items)<=100:
                insert_data()
                print(f"data inserted at {now}, items_updated: {len(items)}, more_updates: {more_updates}")
                
                send_teams_message("data successfully inserted in DB")    
            else:
                print(f"no updates at {now}, items_updated: {len(items)}, more_updates: {more_updates}")
               
                send_teams_message("No updates in DB")  
   except:
        send_teams_message("Exception, data not inserted in DB")  
        
interval(process,15)  

EDIT2:

def process():
   
   data=get_data()
   more_updates=data["Update"]["MoreUpdates"]
   items=data["Update"]["Items"] 
   now=datetime.now()     
   
   try:
        get_data()
        if more_updates is True:
          while more_updates:
            insert_data()
            print()
            send_teams_message()
            get_data()
            data=get_data()
            more_updates=data["Update"]["MoreUpdates"]
          
Asked By: user19441790

||

Answers:

It appears you expect more_updates to change inside the loop by calling get_data() again, since you’ve initialized it by using

data=get_data()
more_updates=data["Update"]["MoreUpdates"]

This is not the case. You’ll have to explicitly assign to more_updates again inside the loop:

while more_updates:
    # ...
    data = get_data()
    more_updates = data["Update"]["MoreUpdates"]

(Also note that the is True part in while more_updates is True is redundant, unless you want to differentiate literal True from other truthy values like non-zero numbers or non-empty collections.)

Answered By: mkrieger1
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.