Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With python.multiprocessing, how do I create a proxy in the current process to pass to other processes?

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?

like image 333
Chris B. Avatar asked Feb 27 '26 15:02

Chris B.


1 Answers

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.

like image 110
drAlberT Avatar answered Mar 01 '26 05:03

drAlberT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!