Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python shelve dbm.error?

Tags:

python

shelve

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

like image 771
mathtick Avatar asked Nov 05 '22 02:11

mathtick


1 Answers

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.

like image 88
Sébastien Pierre Avatar answered Nov 11 '22 05:11

Sébastien Pierre