2

我有一个很大的 lmdb,大约 800K 图像。我只想一一阅读条目。我的代码非常简单,如下所示:

with env.begin() as txn:
cursor = txn.cursor()
for key, value in cursor:
    print(key)

但是在阅读了大约 70000 个条目后,它的内存不足(~10GB)。我不知道为什么。我试着像下面那样做,但没有用。

for r in range(0,env.stat()['entries']):
if r%10000==0:
    if r!=0:
        txn.commit()
        cur.close()
    txn=env.begin()
    cur = txn.cursor()
    print("Change Change Change "+ str(r))
    sys.stdout.flush()
    if r==0:
        cur.first()
    else:
        cur.set_range(key)
        cur.next()
key, value = cur.item()

有什么建议吗?

4

1 回答 1

0

错误跟踪可能会有所帮助。我会检查map_size参数。从文档

最大大小的数据库可能会增长到;用于调整内存映射的大小。如果数据库增长大于 map_size,将引发异常,用户必须关闭并重新打开环境。在 64 位上,使这个巨大(比如 1TB)没有任何惩罚。在 32 位上必须小于 2GB。

这将是一个编写时的示例:

with lmdb.open(LMDB_DIR, map_size=LMDB_MAX_SIZE) as env:
    with env.begin(write=True) as txn:
        return txn.put(mykey, value)
于 2016-05-18T22:40:43.320 回答