I have a custom class in which I need to access a protected attribute on my object of this class, the processing takes too long, so I want to parallelize it, and the data is too large, so I have to do it in a way that the processes share the object instead of copying it around.
I found out about managers on multiprocessing, and I started writing one for my class which led me to an error similar to the one below:
AttributeError: 'AutoProxy[Test]' object has no attribute '_foo'
So I did some research and tried following the code in here which led me to a code similar to the one below:
from multiprocessing.managers import BaseManager, NamespaceProxy
from random import random, randint
import multiprocessing as mp
class TestClass:
def __init__(self):
self._foo = {key: random() for key in range(100)}
class TestManager(BaseManager):
pass
class TestProxy(NamespaceProxy):
_exposed_ = ('__getattribute__', '__setattr__', '__delattr__')
TestManager.register('Test', TestManager, TestProxy)
def print_random(bar):
print(bar._foo[randint(0, 99)])
if __name__ == '__main__':
manager = TestManager()
manager.start()
test = manager.Test()
pool = mp.Pool(mp.cpu_count())
for _ in range(100):
pool.apply(print_random, args=(test,))
pool.close()
pool.join()
But I'm still the following error, and after searching a lot I could not find a way of getting around it:
AttributeError: 'TestProxy' object has no attribute '_foo'
Any idea of what could be happening and how I should fix it?