3

I'm using the multiprocessing library in Python. I can see how to define that objects returned from functions should have proxies created, but I'd like to have objects in the current process turned into proxies so I can pass them as parameters.

For example, running the following script:

from multiprocessing import current_process
from multiprocessing.managers import BaseManager

class ProxyTest(object):
    def call_a(self):
        print 'A called in %s' % current_process()

    def call_b(self, proxy_test):
        print 'B called in %s' % current_process()
        proxy_test.call_a()

class MyManager(BaseManager):
    pass

MyManager.register('proxy_test', ProxyTest)

if __name__ == '__main__':
    manager = MyManager()
    manager.start()

    pt1 = ProxyTest()
    pt2 = manager.proxy_test()

    pt1.call_a()
    pt2.call_a()

    pt1.call_b(pt2)
    pt2.call_b(pt1)

... I get the following output ...

A called in <_MainProcess(MainProcess, started)>
A called in <Process(MyManager-1, started)>
B called in <_MainProcess(MainProcess, started)>
A called in <Process(MyManager-1, started)>
B called in <Process(MyManager-1, started)>
A called in <Process(MyManager-1, started)>

... but I want that final line of output coming from _MainProcess.

I could just create another Process and run it from there, but I'm trying to keep the amount of data that needs to be passed between processes to a minimum. The documentation for the Manager object mentioned a serve_forever method, but it doesn't seem to be supported. Any ideas? Does anyone know?

1 Answer 1

1

Why do you say serve_forever is not supported?

manager = Mymanager()
s = manager.get_server()
s.serve_forever()

should work.

See managers.BaseManager.get_server doc for official examples.

Sign up to request clarification or add additional context in comments.

1 Comment

The docs say that serve_forever belongs to the BaseManager object, when in fact the Server object returned by get_server is the owner of the serve_forever function!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.