I need to pass the proxy of an object to another object but whenever I do this all the other object gets is the referent of the proxy and not the proxy itself. This is something similar to what I am trying to do:
import multiprocessing.managers as m
class Foo(object):
def __init__(self):
self.manager = MyManager()
self.manager.register('Bar', Bar)
self.manager.start()
self.bar = self.manager.Bar()
self.bar.set_proxy(self.bar)
class Bar(object):
def __init__(self):
self.proxy = None
def set_proxy(self, proxy):
self.proxy = proxy
class MyManager(m.BaseManager):
pass
test = Foo()
whenever I do this the value in self.proxy
is the instance of Foo that I created and not the proxy that was returned by the manager.
This is because of design flaw, or perhaps a bug, in the way
Proxy
instances get unpickled. Right now, if the unpickling code see you're running inside aManager
, it gives you the referent when you unpickle, rather than theProxy
:Sometimes this might be desirable, but sometimes its not, like it is in your case. You can work around it by replacing the
ReduceProxy
function that does the unpickling in your program:If you don't want to monkey-patch, you can also subclass
BaseProxy
, though its a bit more work: