python 搁置 dbm.error?
我正在尝试将字典的字典添加到搁置文件中:
>>> d = shelve.open('index.shelve')
>>> d
<shelve.DbfilenameShelf object at 0x21965f0>
>>> print(list(d.keys()))
[]
>>> d['index'] = index
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/bns/rma/local/python/lib/python3.1/shelve.py", line 124, in __setitem__
self.dict[key.encode(self.keyencoding)] = f.getvalue()
_dbm.error: cannot add item to database
索引有点大但不是很大。它本质上是一个浮点数组:
>>> len(index)
219
>>> a = [ index[k][k1] for k in index for k1 in index[k] ]
>>> len(a)
59995
>>> all([ type(x) is float for x in a ])
True
这个错误是什么?另外,模块或模块文档中是否有某个地方我应该寻找有关错误代表的更多信息?错误消息的信息量不是很大,至少对我来说是这样:)。
I'm trying to add a dict of dicts to a shelve file:
>>> d = shelve.open('index.shelve')
>>> d
<shelve.DbfilenameShelf object at 0x21965f0>
>>> print(list(d.keys()))
[]
>>> d['index'] = index
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/bns/rma/local/python/lib/python3.1/shelve.py", line 124, in __setitem__
self.dict[key.encode(self.keyencoding)] = f.getvalue()
_dbm.error: cannot add item to database
index is somewhat large but not huge. It is essentially an array of floats:
>>> len(index)
219
>>> a = [ index[k][k1] for k in index for k1 in index[k] ]
>>> len(a)
59995
>>> all([ type(x) is float for x in a ])
True
What is this error? Also, is there somewhere within the module or the module docs I should be looking to get more info on what the error represents? The error message is not very informative, at least to me :).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,我对 dbm 模块也有同样的问题,它在我的代码库中是可重现的,但我无法在单独的测试中重现它。
我的印象是,有一个锁可以防止在读取数据库时写入。就我而言,数据库约为 200Kb,有约 10 个键,插入
time.sleep(1)
可以解决问题,暗示某些异步过程在时刻尚未完成db[键] = 值
。I actually had the same problem with the dbm module, it is reproducable in my codebase but I can't reproduce it in an isolated test.
My impression is that there is a lock that prevents writing when the database is being read. In my case, the db is ~200Kb, with ~10 keys and inserting a
time.sleep(1)
would solve the problem, hinting at some async process not finished at the moment of thedb[key] = value
.