如何知道 python 多处理管理器正在共享哪些对象?

发布于 2024-12-29 13:01:31 字数 1209 浏览 0 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笏见 2025-01-05 13:01:31

没有什么可以阻止您对类进行内省,并为每个共享属性生成 getter 并调用 register

There's nothing stopping you from introspecting into the class and, for each shared attribute, generating the getter and calling register.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文