I used this stackoverflow question to try to implement a manager
object that would manage the writing of attributes of a custom object. I know that I need to expose the magic methods like __getattribute__
, __setattr__
, and __delattr__
by defining _exposed_
in my proxy class. However, when I try to set the value of an element of a subscriptable attribute of the custom object, it remains unchanged.
Viewing the multiprocessing
documentation, I can't find the multiprocessing.managers
subclass NamespaceProxy
—mentioned in the aforementioned post—anywhere. I am able to import it on one hand; however, I have a nagging doubt that it isn't being implemented correctly.
Here is how I've tried to change the value of an element of an array, attributed to an object of a custom class:
from multiprocessing.managers import BaseManager, NamespaceProxy
import numpy as np
class TestClass(object):
def __init__(self, a):
'''
Args:
a (np.ndarray): the array that needs to be changed
'''
self.a = a
class TestProxy(NamespaceProxy):
# exposes the magic methods for TestProxy objects needed for setting their attributes
_exposed_ = ('__getattribute__', '__setattr__', '__delattr__')
class MyManager(BaseManager):
pass
if __name__ == '__main__':
MyManager.register('test', TestClass, TestProxy)
manager = MyManager()
manager.start()
arr = np.array([0,0,0,0])
managed_obj = manager.test(arr)
managed_obj.a[0] = 1
print(managed_obj.a)
# Console: [0 0 0 0]
# Expected ouput: [1 0 0 0]
Edit: I was able to change the actual value of a
with something like
arr2 = np.array([0])
managed_obj.a = arr2
print(managed_obj.a)
# Console : [0]
However, I still don't know how to change the value of an element of a
.