防止python搁置损坏 [英] Prevent python shelve corruption

查看:93
本文介绍了防止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?

推荐答案

如果持久性对象的安全性在您的项目中至关重要,那么使用shelve并不是一个好主意.腌制对象并手动将它们写入文件都不会.

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.

请考虑一下,实际数据库会投入大量资源(包括强大的功能和代码)来确保万一发生故障时的安全.因此,将数据保存在真实的数据库中.最简单的是sqlite,因为它与Python捆绑在一起. sqlite是相当安全的,并且即使在系统出现故障(例如有人用PC的电源线跳闸)时,也可以保持数据处于有效状态,这方面有很多技巧.

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).

这篇关于防止python搁置损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆