同步但不关闭 dbm

Syncing but not closing a dbm

这样做有问题吗:

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)

并在睡眠时间用CTRL + C中断程序,即dumbdbm 不会正常关闭 ?

dumbdbm.sync()是否足以保证数据安全,还是.close()是绝对强制性的?

documentation implies 同步就足够了,它说调用该方法会同步磁盘目录和数据文件。

不过,我认为这里更好的方法是在退出前关闭文件。如果你总是以 Ctrl-C 退出,你可以通过为 SIGINT 注册一个信号处理程序(这是 Ctrl-C 发送的信号)来实现这一点。这个信号处理程序应该同步,关闭数据库,然后调用 exit()。