0

Here is my code. I am not getting why it is giving the Error: AttributeError: 'AutoProxy[MySharedClass]' object has no attribute 'dict_' import time import multiprocessing from multiprocessing import Process, Manager import multiprocessing.managers as manager

class MySharedClass(object):
    def __init__(self):
        self.dict_ = {}
        self.dict_['one'] = 1
        self.dict_['two'] = 2

class MyManager(manager.BaseManager):
    pass

def test_process(param_object):
    print"Child Process:"
    print param_object.dict_
    param_object.dict_['three'] = 3
    print param_object.dict_
    return param_object


def main_process():
    MyManager.register("MySharedClass", MySharedClass)
    my_manager = MyManager()
    my_manager.start()

    param_object = my_manager.MySharedClass()
    print"Main process:"
    print param_object.dict_
    time.sleep(.2)
    proc = Process(target=test_process, args=(param_object,))
    proc.start()
    print"Process created with id %s" %proc.pid

    time.sleep(10)

    print "Main process End:"
    print param_object.dict_

main_process()
Rahul.Shikhare
  • 179
  • 1
  • 16
  • I want share the object of MySharedClass() to child process and manipulate it by child process – Rahul.Shikhare Mar 01 '16 at 10:27
  • Which version of Python, what OS are you running on? – DisappointedByUnaccountableMod Mar 01 '16 at 10:34
  • Python 2.7.6 and OS is Ubuntu 14.04 – Rahul.Shikhare Mar 01 '16 at 10:51
  • I don't think that (however much you want it to) you can use manager like this with an arbitrary class - see http://stackoverflow.com/questions/10721915/shared-memory-objects-in-python-multiprocessing and http://stackoverflow.com/questions/1268252/python-possible-to-share-in-memory-data-between-2-separate-processes/1269055#1269055 and here https://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes – DisappointedByUnaccountableMod Mar 01 '16 at 10:55
  • Is it possible to create shared object of class MySharedClass() if some of the attributes are initialized by calling REST API – Rahul.Shikhare Mar 01 '16 at 10:58
  • if you mean, can you implement a REST API to access object values and invoke operations, then yes, why not. If by shared you mean you want more than one client to modify it then you still have to be careful in the design of your server and clients to make that parallel access robust. – DisappointedByUnaccountableMod Mar 01 '16 at 11:06
  • Currently I am working on OpenStack Cloud operating system.... and MySharedClass() class have the variables which are initialized by calling OpenStack REST API's like KeystoneAPI's to get user authentication from Keystone component of OpenStack. After successful creation of object of MySharedClas() I need it to share this object with worker process where worker can manipulate some of the object variable and it is visible to all other worker process. So is it possible to create such a complex object which are shareable between worker processes ? – Rahul.Shikhare Mar 01 '16 at 11:24
  • Not technically impossible, I don't suppose, but don't know how difficult. Good luck. – DisappointedByUnaccountableMod Mar 01 '16 at 11:31

0 Answers0