I have the following problem. I have written a function that takes a list as input and creates a dictionary for each element in the list. I then want to append this dictionary to a new list, so I get a list of dictionaries. I am trying to spawn multiple processes for this. My problem here is that I want the different processes to access the list of dictionaries as it is updated by other processes, for example to print something once the has reached a certain length.
My example would be like this:
import multiprocessing
list=['A', 'B', 'C', 'D', 'E', 'F']
def do_stuff(element):
element_dict={}
element_dict['name']=element
new_list=[]
new_list.append(element_dict)
if len(new_list)>3:
print 'list > 3'
###Main###
pool=multiprocessing.Pool(processes=6)
pool.map(do_stuff, list)
pool.close()
Right now my problem is that each process creates its own new_list
. Is there a way to share the list between processes, such that all dictionaries are appended to the same list? Or is the only way to define the new_list
outside of the function?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…