如何知道 python 多处理管理器正在共享哪些对象?
在Python多处理模块中,为了从远程管理器获取对象,大多数食谱告诉我们需要构建一个getter来恢复每个对象:
class QueueMgr(multiprocessing.managers.SyncManager): pass
datos=Queue()
resultados=Queue()
topList=list(top)
QueueMgr.register('get_datos',callable=lambda:datos)
QueueMgr.register('get_resultados',callable=lambda:resultados)
QueueMgr.register('get_top',callable=lambda:topList)
def Cola_run():
queueMgr=QueueMgr(address=('172.2.0.1', 25555),authkey="foo")
queueMgr.get_server().serve_forever()
Cola=Thread(target=Cola_run)
Cola.daemon=True
Cola.start()
并且必须在客户端程序中声明相同的getter:
class QueueMgr(multiprocessing.managers.SyncManager): pass
QueueMgr.register('get_datos')
QueueMgr.register('get_resultados')
QueueMgr.register('get_top')
queueMgr=QueueMgr(address=('172.22.0.4', 25555),authkey="foo")
queueMgr.connect()
datos=queueMgr.get_datos()
resultados=queueMgr.get_resultados()
top=queueMgr.get_top()._getvalue()
好的,它涵盖了大部分使用案例。但我发现代码看起来很丑。也许我没有得到正确的食谱。但如果确实如此,那么至少我可以在客户端中编写一些更好的代码,也许可以自动声明 getter,如果我能够提前知道 Manager 正在共享哪些对象。他们有办法做到这一点吗?
如果您认为 multiprocessing.Manager() 提供的 SyncManager 实例允许创建复杂的 Proxy 对象,但任何连接到此类 SyncManager 的客户端似乎都需要从其他地方获取对此类代理的引用,则尤其令人不安。
In python multiprocessing module, in order to obtain an object from a remote Manager, most recipes tell us that we need to build a getter to recover each object:
class QueueMgr(multiprocessing.managers.SyncManager): pass
datos=Queue()
resultados=Queue()
topList=list(top)
QueueMgr.register('get_datos',callable=lambda:datos)
QueueMgr.register('get_resultados',callable=lambda:resultados)
QueueMgr.register('get_top',callable=lambda:topList)
def Cola_run():
queueMgr=QueueMgr(address=('172.2.0.1', 25555),authkey="foo")
queueMgr.get_server().serve_forever()
Cola=Thread(target=Cola_run)
Cola.daemon=True
Cola.start()
and than the same getter must be declared in the client program:
class QueueMgr(multiprocessing.managers.SyncManager): pass
QueueMgr.register('get_datos')
QueueMgr.register('get_resultados')
QueueMgr.register('get_top')
queueMgr=QueueMgr(address=('172.22.0.4', 25555),authkey="foo")
queueMgr.connect()
datos=queueMgr.get_datos()
resultados=queueMgr.get_resultados()
top=queueMgr.get_top()._getvalue()
Ok, it covers most usage cases. But I find the code looks ugly. Perhaps I am not getting the right recipe. But if it is really so, then at least I could do some nicer code in the client, perhaps automagically declaring the getters, if I were able to known in advance what objects the Manager is sharing. Is they a way to do it?
It is particularly troubling if you think that the instances of SyncManager provided by multiprocessing.Manager() allow to create sophisticated Proxy objects but that any client connecting to such SyncManager seems to need to obtain the reference to such proxies from elsewhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有什么可以阻止您对类进行内省,并为每个共享属性生成 getter 并调用
register
。There's nothing stopping you from introspecting into the class and, for each shared attribute, generating the getter and calling
register
.