transaction-like file operations
jonathan at onegoodidea.com
Thu Aug 8 09:26:38 CEST 2002
On 7/8/2002 10:27, in article 3d50e6c3.100966296 at News.CIS.DFN.DE, "Gerson
Kurz" <moc.q-dnan-p at p-nand-q.com> wrote:
> So, what I need is a transaction-like file operation, that allows me
> to either write the file completely, or keep the old file (so that at
> every point there is at least one set of data available).
> I have made a "homegrown" solution that includes writing to a backup
> file first, then doing two rename operations. I wonder if there exists
> a standard class for transaction-like file operations in python?
> [Note: a database is not an option]
What you're doing sounds pretty much the right solution. I don't think
you'll find any standard Python class/module for doing this kind of thing,
but it's not difficult to write so I wouldn't worry about it.
The only other thing I can think of is to not do the rename operations,
which theoretically could be interrupted/fail leaving both files
inaccessible. Instead I'd change the boot-time read operation to look for
two different possible filenames and load the one with the newer datestamp.
[This assumes your flash filesystem has file timestamps - if not, you can
use incrementing filenames instead, like 'data0001', 'data0002', etc.]
At boot-time, when you load this file, check a checksum on it to verify it
was successfully read. If it loads OK, then unlink the older file. If
loading the file fails, or the checksum fails, then unlink the new file and
load the older one instead.
When you save a new file you write it with the other filename to the one you
loaded at boot time (and write a checksum with it).
(I'm sure I used a Cisco router that did a variation on this in the past.)
More information about the Python-list