I have created python script with multithreads, each thread write value to global dict which is thread safe because each thread update the dictionary with new unique value that didn't exist before, I want each thread to save the results of the dict in the output file, but i receive "dictionary changed size during iteration", is there a way to do that like locking the dictionary for writing during dumping to a file, i tried to lock and release but didn't work
def do_function():
while True:
r=q.get()
global_dict[r]={} --> this is thread safe as r is unique it will not repeat again
telephone,address=get_info(r)
global_dict[r]['t']=telephone
global_dict[r]['a']=address
with open("output.pickle","wb") as j: --> save to file
pickle.dump(global_dict,j) --> receive error dictionary changed size during iteration
q.task_done()
global dict={}
thread=10
q = Queue(threads * 2)
for i in range(concurrent):
t = Thread(target=do_function)
t.daemon = True
t.start()
for p in lst:
q.put(p)
q.join()