开发者

python shelve dbm.error?

开发者 https://www.devze.com 2023-02-11 12:33 出处:网络
I\'m trying to add a dict of dicts to a shelve file: >>> d = shelve.open(\'index.shelve\') >>> d

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 :).


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 the db[key] = value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号