python Manager 之dict KeyError问题的解决

yipeiwu_com6年前Python基础

程序需要多进程见共享内存,使用了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设计】。

相关文章

Python中字符串的常见操作技巧总结

本文实例总结了Python中字符串的常见操作技巧。分享给大家供大家参考,具体如下: 反转一个字符串 >>> S = 'abcdefghijklmnop' >&...

PyTorch 解决Dataset和Dataloader遇到的问题

今天在使用PyTorch中Dataset遇到了一个问题。先看代码 class psDataset(Dataset): def __init__(self, x, y, trans...

Python3通过Luhn算法快速验证信用卡卡号的方法

本文实例讲述了Python3通过Luhn算法快速验证信用卡卡号的方法。分享给大家供大家参考。具体分析如下: Python3通过Luhn算法快速验证信用卡卡号,python用起来就是爽,很...

在python中获取div的文本内容并和想定结果进行对比详解

div的内容为: <div style="background-color: rgb(255, 238, 221);" id="status" class="errors">...

python中pandas.DataFrame排除特定行方法示例

前言 大家在使用Python进行数据分析时,经常要使用到的一个数据结构就是pandas的DataFrame,关于python中pandas.DataFrame的基本操作,大家可以查看这篇...