我正在使用multiprocessing
Python 中的库。我可以看到如何定义从函数返回的对象应该创建代理,但我希望将当前进程中的对象变成代理,以便我可以将它们作为参数传递。
例如,运行以下脚本:
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)
...我得到以下输出...
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)>
...但我希望最后一行输出来自_MainProcess
.
我可以创建另一个进程并从那里运行它,但我试图将需要在进程之间传递的数据量保持在最低限度。该Manager
对象的文档提到了一种serve_forever
方法,但似乎不受支持。有任何想法吗?有人知道吗?