0

I'm trying to share an object between 2 processes on two different computers using the Python multiprocessing module.

The problem is that the object never seems to be reflected correctly in the processes.

Referring to the documentation

https://docs.python.org/2.7/library/multiprocessing.html#multiprocessing.managers.SyncManager

and

https://docs.python.org/3/library/multiprocessing.html#multiprocessing.managers.Namespace

I believe this will create an object that is shared between the two clients but this doesn't seem to be the case

class BosunManager(SyncManager):

    def __init__(self, server=True, **kwargs):
        # () -> None
        super(BosunManager, self).__init__(**kwargs)

    def register_functions(self):
        self.x = self.Namespace()

Server Starting program

bosun_manager = BosunManager(address=('', 50020), authkey='abracadabra')
bosun_manager.start()
bosun_manager.register_functions()

Client Program #1

from bosun.multi_process_manager import BosunManager
bm = BosunManager(server=False, address=('', 50020), authkey='abracadabra')


bm.connect()

bm.register_functions()
bm.x.x =10 # <---- SETTING X HERE

Client Program #2

from bosun.multi_process_manager import BosunManager

bm = BosunManager(server=False, address=('', 50020), authkey='abracadabra')
bm.connect()
bm.register_functions()
print bm.x.x

However Client #2 always shows an AttributeError for the print bm.x.x line

cjds
  • 8,268
  • 10
  • 49
  • 84
  • Have you taken a look at this answer yet? https://stackoverflow.com/questions/3671666/sharing-a-complex-object-between-python-processes – chang_trenton Jul 29 '19 at 00:21
  • Possible duplicate of [Sharing a complex object between Python processes?](https://stackoverflow.com/questions/3671666/sharing-a-complex-object-between-python-processes) – chang_trenton Jul 29 '19 at 00:21
  • @tchainzzz yeah I've seen it. The code above is a specific implementation of what that answer suggests that you do. It suggests using `Manager`. I've used a subclass called `SyncManager` which should have an automatically resolving `Namespace` as mentioned [here](https://docs.python.org/2.7/library/multiprocessing.html?highlight=multiprocessing.manager#multiprocessing.managers.Namespace) but doesn't seem to work correctly with the way it's currently written and I'm not sure why – cjds Jul 29 '19 at 00:33

0 Answers0