5

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

mathtick
  • 6,487
  • 13
  • 56
  • 101
  • I should have also said that I'm using python 3.1.2. – mathtick Feb 14 '11 at 19:13
  • Can you set elements of the shelve at all? For example, if `index` is an int, do you get the same crash? What about a dict, or a smaller dict of dicts? – Andrew Feb 14 '11 at 19:34
  • I should have mentioned that ... it appears to work fine for a small list or dict that I tested it with. – mathtick Feb 15 '11 at 18:02
  • I'm interested in the boundary between a not-crashing version and a crashing version. Can you find a case where a dict with N entries doesn't crash, but N+1 does? – Andrew Feb 22 '11 at 05:01
  • Good point. I will try to test this soon and post back. – mathtick Mar 01 '11 at 04:22

1 Answers1

2

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.

  • Here are links to similar issues: http://issues.roundup-tracker.org/issue1017587 and http://markmail.org/message/joqtmews3iwkcvtu#query:+page:1+mid:joqtmews3iwkcvtu+state:results – Sébastien Pierre Aug 28 '12 at 20:39