我需要在多个进程之间共享一个巨大的字典(大小约为1 GB),但是因为所有进程都将始终从中读取.我不需要锁定.
有没有办法在没有锁定的情况下共享字典?
python中的多处理模块提供了一个Array类,它允许通过设置
lock = false 来进行无锁定共享.
但是在多处理模块中管理器提供的Dictionary没有这样的选项.
def maxVote(nLabels):
count = {}
maxList = []
maxCount = 0
for nLabel in nLabels:
if nLabel in count:
count[nLabel] += 1
else:
count[nLabel] = 1
#Check if the count is max
if count[nLabel] > maxCount:
maxCount = count[nLabel]
maxList = [nLabel,]
elif count[nLabel]==maxCount:
maxList.append(nLabel)
return random.choice(maxList)
Run Code Online (Sandbox Code Playgroud)
nLabels 包含整数列表.
上面的函数返回具有最高频率的整数,如果多于一个具有相同的频率,则返回从它们中随机选择的整数.
例如maxVote([1,3,4,5,5,5,3,12,11])是5