程序需要多进程见共享内存,使用了Manager的dict。最初代码如下:
from multiprocessing import Process, Manager d = Manager().dict() d2 = {} def f(): d['a1'] = {} <span style="color:#ff6666;"> d['a1']['a2'] = 11 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 print d['a1']['a2'] if __name__ == '__main__': p = Process(target=f) p.start() p.join()
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/7423.html