【发布时间】:2020-05-27 12:27:18
【问题描述】:
我在 python 3.7 中使用multiprocessing 库。
在我的用例中,我从dicts 中的普通list 开始 - 我使用它从SyncManager 创建一个ListProxy 对象,然后在池中使用它来同步list (创建如下)。
data = [{...},{...},{...}] # arbitrary list of dicts that I want to sync in a pool
with Manager() as manager:
list_proxy = manager.list()
for row in data:
list_proxy.append(manager.dict(row))
# ... list_proxy used in pool.starmap later
我的问题:有没有一种 Pythonic 方法可以在池完成运行后将我的 ListProxy 更改回正常列表?我查看了文档,甚至查看了源代码,似乎唯一的方法可能是使用 BaseProxy 类中的 _getvalue() 方法 - 但这似乎很老套,我还必须递归地“取消代理”任何嵌套的代理对象。
我需要在后面的代码中返回正常列表以将其序列化为 json,ProxyObjects 不容易序列化。
【问题讨论】:
标签: python python-3.x python-multiprocessing multiprocessing-manager