我有一些数据存储在数据库中,我想处理。所以在数据库加载之前我就决定了所有的数据都在缓慢的加载。但是,由于存储的数据太大,我遇到内存不足的错误(我看到使用了超过2个gig)。所以我决定使用磁盘数据结构,并发现使用shelve是一种选择。下面是我要做的(伪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)我真的需要写回=True吗?它有什么作用?在
2)我仍然会遇到OutofMemory异常,因为我无法控制何时将数据写入磁盘。我该怎么做?我试着每隔几次迭代执行一次sync(),但也没用。在
谢谢!在