pythonpython-2.7dictionaryflushdbm

Syncing but not closing a dbm


Is it a problem to do :

import time
import dumbdbm

db = dumbdbm.open('db.db', 'c')

# modify the persistent dict / "DB" here
db['foo'] = 'bar'
db.sync()        

while True:
    # doing other things, sometimes modifying the db + syncing with .sync()
    time.sleep(1)

and to break the program with CTRL + C during the sleeping time, i.e. the dumbdbm will not be properly closed ?

Is dumbdbm.sync() enough to guarantee safety of the datas, or is .close() absolutely mandatory?


Solution

  • The documentation implies syncing is enough when it says that calling that method synchronized the on-disk directory and data files.

    However, I think the better approach here is to close the file before exiting. If you're always exiting with Ctrl-C, you can make this happen by registering a signal handler for SIGINT (which is the signal sent by Ctrl-C.) This signal handler should sync, close the DB, then call exit().