KeyError when trying to access a key value from a nested Manager.dict

I have multiprocessor code in which I would like to share a nested dictionary among processes. A dictionary is never modified by processes; just read it.

In its 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 KeyErrora print statement that says that the key I'm using does not exist. I am not sure why this is happening since I just inserted the key in the dict. When I change this to a regular dict, the problem disappears.

+4
source share
1 answer

, Manager.list, dict, 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]))
+3

Source: https://habr.com/ru/post/1610205/


All Articles