I understand that you can't remove an element from a list if you are currently iterating over that list. What I'm trying to do then is to copy elements from that list that I do NOT wish to remove to another list, and then replacing the original list with the new list. Here's my relevant code:
while len(tokenList) > 0:
# loop through the tokenList list
# reset the updated token list and the remove flag
updatedTokenList = []
removeFlag = False
for token in tokenList:
completionHash = aciServer.checkTaskForCompletion(token)
# If the completion hash is not the empty hash, parse the information
if completionHash != {}:
# if we find that a task has completed, remove it from the list
if completionHash['Status'] == 'FINISHED' and completionHash['Error'] == '':
# The task completed successfully, remove the token from the list
removeFlag = True
elif completionHash['Status'] == 'RUNNING' and completionHash['Error'] == '':
# The task must still be running
print('Task ' + completionHash['Type'] + ' ' + token + ' has been running for ' + completionHash['Runtime'] + ' seconds.')
elif completionHash['Status'] == 'queued':
# The task is in the queue
print('Task ' + completionHash['Type'] + ' ' + token + ' is queued in position ' + completionHash['QueuePosition'])
elif completionHash['Status'] == 'not_found':
# Did not find a task with this token, possible the task hasn't been added yet
print(completionHash['Error'])
# if the task is still running, no change to the token list will have occured
else:
# This is probably because the server got rid of the token after the task completed
print('Completion hash is empty, something went wrong.')
tokenListError.append(token)
removeFlag = True
if not removeFlag:
print('appending token to updatedTokenList')
updatedTokenList.append(token)
print('length of tokenList after removal loop: ' + str(len(updatedTokenList)))
# wait some time, proportional to the number of tasks left
checkInterval = len(updatedTokenList) * checkIntervalMultiplier
print('Waiting ' + str(checkInterval) + ' seconds before checking again...')
print('Tokens remaining: ' + str(len(updatedTokenList)))
# replace the original token list with the updated token list
tokenList = updatedTokenList
# wait a while based on how many tokens remain
time.sleep(checkInterval)
So point of all this is to update the tokenList with new list. Every time through the loop, new tasks will have finished and they should NOT be added to the updatedTokenList. The remaining task tokens will and this replaces the original token list.
This does not work. On my first pass through, it does not add any tokens to the updatedTokenList even though no tasks have yet completed. I cannot figure out what I am doing wrong. Any suggestions?
removeFlagtoFalseat the beginning of each iteration of the loop. It could be that the first task is complete and sinceremoveFlagis never reset toFalseyou remove all of the tasks. If that doesn't fix your problem, try stepping through your code and seeing whatcheckTaskForCompletion()returns, verifying you have the correctifconditions.