How do I write to a parent’s dictionary? I’ve given a list of urls to some children to retrieve, which then need to write to the parent’s dictionary:
from multiprocessing import Pool
import random
parent_dict={}
urls = ['www.yahoo.com','www.google.com','www.microsoft.com','www.apple.com','www.cisco.com']
def workit(url):
# retrieve the urls, process some stuff and then add that info to parent_dict
key = random.randrange(1,10) # pretend that this is the variable that we want in parent_dict
value = random.randrange(11,20)
parent_dict[key] = value
pool = Pool(processes = 5)
pool.map(workit,urls)
print parent_dict # returns {}}
The following code is more or less yours, adapted with to deal with passing around the
manager.dictthat is mentioned in the python multiprocessing docs.There are a couple of things to note here that are critical issues:
idle. You’ll want to run it from a command line using python .py instead.Good luck with whatever you decide. Don’t use my code. It runs and won’t give you malware but that’s about all it’s got in its favor.