我正在尝试将 JSON 对象加载到 DictProxy
对象中,但没有看到“简单”的方法。我可以很容易地将 JSON 加载到字典中,但如果我尝试将此 dict
复制到 DictProxy
中,它仍然是 dict
(这对我的做法是有意义的)。我可以创建一个空的 DictProxy
,然后构建一个循环遍历我的 dict
并填充 DictProxy
的函数,但我觉得这应该是不必要的。我也找不到太多关于 DictProxy
的文档,所以我不太确定它与 dict
共享(或不共享)哪些功能。有没有一种干净的方法来做到这一点,或者我应该构建循环遍历 dict
并构建 DictProxy
的函数?
代码:
import multiprocessing
import json
def main():
with open(r"c:\path\to\my.json") as js:
my_dict = json.load(js)
print type(my_dict) # <type 'dict'>
manager = multiprocessing.Manager()
my_dict_proxy = manager.dict()
print type(my_dict_proxy) # <class 'multiprocessing.managers.DictProxy'>
my_dict_proxy = my_dict
print type(my_dict_proxy) # <type 'dict'>
if __name__ == "__main__":
main()
最佳答案
DictProxy
提供了 update()
方法。您应该能够执行以下操作:
manager = multiprocessing.Manager()
my_dict_proxy = manager.dict()
my_dict_proxy.update(my_dict)
关于python - 将 JSON 加载到 multiprocessing.managers.DictProxy 对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49804815/