dbm string lengths

Linus Chang somaking at yoyo.cc.monash.edu.au
Sun Apr 23 23:28:45 EDT 2000


Hi all,

I'm trying to use anydbm to store potentially long strings, and am running
into problems which seem to be caused by the length of the strings. (The
Python Ref Manual sec 3.11 on Shelve mentions that the length of pickled
objects should be small - how small?)

eg. the following code fragment:

import anydbm
import cPickle

db = anydbm.open("testingdb", "c")

print "Keys in db are: ",
for k in db.keys():
    print k,
list = []

for x in range(10000):
    list.append("Item number " + `x`)

db["item"] = cPickle.dumps(list)
db.sync()
db["item"] = "Small string"
db.sync()

-- Run the first time (no testingdb file exists), output as expected:
D:\Program Files\Python>python dbtest.py
Keys in db are:
-- Run a second time, the name of the key gets confused:
D:\Program Files\Python>python dbtest.py
Keys in db are:  itemm

(Changing the list to say a length of 10 yields results as expected.)

Alternatively, removing the line db["item"] = "Small string" causes the
following output on the second run:

D:\Program Files\Python>python dbtest.py
Keys in db are:
Traceback (innermost last):
  File "dbtest.py", line 8, in ?
    for k in db.keys():
bsddb.error: (22, 'Invalid argument')

-- Can anyone please help? I'm trying to store potentially long strings in
a dictionary, and need to store them to disk. I expect that entries will
be updated at up to 20 per second, so it's not really feasible to pickle
the whole dictionary to disk every time an individual entry is updated. In
addition, saving to disk asynchronously (eg. every X seconds) really isn't
my preference...

Thanks in advance,

Linus Chang





More information about the Python-list mailing list