I have some multiprocessing code where I'd like to share a nested dictionary among processes. The dictionary is never modified by the processes; just read.
In the simplest form, the problematic code is as follows:
from multiprocessing import Manager
class MyClass(object):
def __init__(self):
self.manager = Manager()
self.delays = self.manager.dict({})
def foo(self, types, keys):
for type in types:
self.delays[type] = self.manager.dict({})
for key in keys:
self.delays[type][key] = 0
print("The delay is " + str(self.delays[type][key]))
I get a KeyError
at the print statement where it says that the key I'm using does not exist. I'm not sure why this is happening, since I just inserted the key into the dict. When I change this into a regular dict, the problem goes away.
Based on this answer to a related question, you can use a Manager.list
appending a dict, then use a reference to the dict:
from multiprocessing import Manager
class MyClass(object):
def __init__(self):
self.manager = Manager()
self.l = self.manager.list()
self.l.append({})
self.delays = self.l[0]
def foo(self, types, keys):
for type in types:
self.delays[type] = self.manager.dict()
for key in keys:
self.delays[type].setdefault(key, 0)
print("The delay is {}".format(self.delays[type][key]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With