avoiding file corruption
Bryan Olson
fakeaddress at nowhere.org
Sun Aug 27 11:00:10 EDT 2006
Paddy wrote:
> I've never done this in anger so feel free to mock (a little :-).
>
> I'd have a fixed field at the beginning of the field that can hold the
> hostname process number, and access time of a writing process, togeher
> with a sentinal value that means "no process has access to the file".
>
> A program would:
> 1. wait a random time.
> 2. open for update the file
> 3. read the locking data
> 4. If it is already being used by another process then goto 1.
> 5. write the process's locking data and time into the lock field.
> 6 Modify the files other fields.
> 7 write the sentinal value to the locking field.
> 8. Close and flush the file to disk.
That doesn't really work; you have still have a race condition.
Locking the file is the good solution, but operating systems
vary in how it works. Other reasonable solutions are to re-name
the file, work with the renamed version, then change it back
after closing; and to use "lock files", which Wikipedia explains
near the bottom of the "File locking" article.
--
--Bryan
More information about the Python-list
mailing list