防止 python 搁置损坏
我应该如何防止搁置文件损坏?是否应该在大部分时间关闭搁架,然后仅在需要读取或编辑值时才打开?
How should I prevent corruption in a shelve file? Should the shelve be closed most of the time and then opened only when I need to read or edit a value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果持久对象的安全性在您的项目中非常重要,那么使用
shelve
并不是一个好主意。酸洗对象并手动将它们写入文件也不是。考虑到真实的数据库会投入大量资源(脑力和代码)以确保发生故障时的安全。因此,将您的数据保存在真实的数据库中。最简单的是
sqlite
,因为它与 Python 捆绑在一起。sqlite
非常安全,并且具有很多智能功能,即使在系统发生故障时(例如有人被 PC 的电源线绊倒),也可以将数据保持在某种有效状态。If safety of your persistent objects is of high importance in your project, using
shelve
is not a good idea. Neither is pickling objects and manually writing them into files.Consider that real databases invest huge resources (brainpower and code) to be safe in case of failures. So keep your data in a real DB. The simplest would be
sqlite
, as it comes bundled with Python.sqlite
is quite safe and has a lot of smarts in it about keeping your data in some valid state even in case of system failures (like when someone trips on your PC's power cable).