Python死锁错误 - 尝试从搁置中检索键值

时间:2013-05-19 03:52:40

标签: python cgi

这是我的代码,我在shelve中更新记录

def updateRecord(db, form):
    if not 'key' in form:
        fields = dict.fromkeys(fieldnames, '?')
        fields['key'] = 'Missing key input'
    else:
        key = form['key'].value
        if key in db:
            record = db[key]
        else:
            from person import Person
            record = Person(name='?',age='?')
        for field in fieldnames:
            setattr(record, field, eval(form[field].value))
        db[key] = record
        fields = record.__dict__
        fields['key'] = key
    return fields

当我尝试从搁置中检索值时,我收到此错误

>>> import shelve
>>> db = shelve.open('class-shelve')
>>> db['sue'].name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/shelve.py", line 121, in __getitem__
    f = StringIO(self.dict[key])
  File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in __getitem__
    return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
  File "/usr/lib/python2.7/bsddb/dbutils.py", line 68, in DeadlockWrap
    return function(*_args, **_kwargs)
  File "/usr/lib/python2.7/bsddb/__init__.py", line 270, in <lambda>
    return _DeadlockWrap(lambda: self.db[key])  # self.db[key]
KeyError: 'sue'

有什么见解?

1 个答案:

答案 0 :(得分:1)

假设在第一个片段中,db变量是一个“架子”对象,那么,虽然该行......

db[key] = record

...会将新的键/值对添加到'shelf',它不一定将内容刷新到磁盘,因此共享相同的'shelf文件'的其他进程将无法使用它。< / p>

您可以通过添加行...

强制将“shelf文件”写入磁盘
db.sync()

...添加新的键/值对后,但当“架子文件”变大时,它可能会非常慢,所以您可能不想太频繁地调用它。