Python 正在同步但未关闭dbm

Python 正在同步但未关闭dbm,python,python-2.7,dictionary,flush,dbm,Python,Python 2.7,Dictionary,Flush,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.slee

这样做是否有问题:

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
将无法正确关闭

是否足以保证数据的安全,或者
.close()
是绝对必需的?

如果说调用该方法同步了磁盘上的目录和数据文件,那么同步就足够了


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

您可以在睡觉前关闭。@ScottHunter,这是我整个事情的简化代码。我修改了。谢谢@gbe。你能举出文件的哪一部分说这已经足够了吗?我已经编辑了我的答案,包括对文件那一部分的引用。