This is the main code:
if __name__ == '__main__':
manager = Manager()
mylist = manager.list()
mydict = manager.dict()
mylist.append('abc')
mylist.append('xyz')
for k in mylist:
mydict[k] = manager.list()
pool = Pool(processes = 100)
pool.map(func, arg)
pool.terminate()
This is the func() definition:
for x in mylist:
r = {}
r['m'] = 1
r['n'] = 2
print (r) # gives correct values
mydict[x].append(dict(r))
print (mydict) # gives empty list, keys are printed correct
As commented in the code, it is printing empty list instead of expected values. How do I make this shared dict and list work properly?
First print is showing expected values. Second print shows expected keys but the value part is empty list. Since it shows the keys correctly, the sharing is working. There is some issue with the append part. I'm unable to figure it out.
Down voters, please do read the linked question carefully before you assume that my question is same as that one. If you still insist they are the same, please leave a comment stating why you think so. From my viewpoint, both questions are different. Linked question asks about how to share a dict with answers suggesting use of Manager.dict(). My question already uses Manager.dict() and is failing because of an anomaly. Thank you.
As can be seen from the comments in the code, mydict
is correctly read inside func()
but fails to mutate it. Manger.dict() object can't mutate nested parts.
It can be fixed by creating a new dictionary and pointing mydict
to it after mutation.
for x in mylist:
r = {}
r['m'] = 1
r['n'] = 2
print (r)
foo = mydict[x]
foo.append(dict(r))
mydict[x] = foo
print (mydict)