Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 226
Vivin Paliath Avatar asked Oct 19 '22 01:10

Vivin Paliath


1 Answers

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]))
like image 105
Padraic Cunningham Avatar answered Oct 21 '22 16:10

Padraic Cunningham