我有一些数据存储在我想要处理的数据库中。数据库访问非常缓慢,因此我决定在进行任何处理之前将所有数据加载到字典中。但是,由于存储的数据量很大,我遇到了内存不足错误(我看到使用了超过 2 个演出)。所以我决定使用磁盘数据结构,并发现使用搁置是一种选择。这就是我所做的(伪python代码)
def loadData():
if (#dict exists on disk):
d = shelve.open(name)
return d
else:
d = shelve.open(name, writeback=True)
#access DB and write data to dict
# d[key] = value
# or for mutable values
# oldValue = d[key]
# newValue = f(oldValue)
# d[key] = newValue
d.close()
d = shelve.open(name, writeback=True)
return d
我有一些问题,
1) 我真的需要 writeBack=True 吗?它有什么作用?
2) 我仍然遇到 OutofMemory 异常,因为我无法控制数据何时写入磁盘。我怎么做?我尝试每隔几次迭代执行一次 sync() ,但这也无济于事。
谢谢!