I have a Python application where thread1 calls an API to see 'what reports are ready to download' and sends that report_id
to thread2 which 'downloads/processes those reports. I am trying to determine what happens if thread1 is adding items to the dict if thread2 is iterating over it. Right now I do a copy before working in thread2
Two questions
Can I iterate over a changing dictionary? I currently a) make a copy of dict before I iterate, b) iterate over a copy of the dict, c) for items that are 'processed' by the loop on the copy of the dict I delete the key from the 'original' dict so on the next loop it doesn't reprocess the same item
If I can't iterate over a changing dict do I need to use a lock to make a copy like I am doing below. Is that the right way to do it?
lock = threading.Lock()
while True:
with lock: #copy dict to prevent contenion
reports_to_call_copy = self.reports_to_call.copy()
for line in reports_to_call_copy:
#do stuff and delete items from original dict so on next loop it doesn't process twice.
is_killed = self._kill.wait(180)
if is_killed:
print("Killing - Request Report")
break
del self.reports_to_call[user, report_name]