python Manager 之dict KeyError问题的解决
程序需要多进程见共享内存,使用了Manager的dict。
最初代码如下:
from multiprocessing import Process, Manager d = Manager().dict() d2 = {} def f(): d['a1'] = {} <span style="color:#ff6666;"> d['a1']['a2'] = 11</span> print d['a1']['a2'] if __name__ == '__main__': p = Process(target=f) p.start() p.join()
结果报错:
print d['a1']['a2'] KeyError: 'a2'
解决方案:
from multiprocessing import Process, Manager d = Manager().dict() d2 = {} def f(): d['a1'] = {} <span style="color:#ff6666;"> d2['a2'] = 11 d['a1'] = d2</span> print d['a1']['a2'] if __name__ == '__main__': p = Process(target=f) p.start() p.join()
以上这篇python Manager 之dict KeyError问题的解决就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。