How to safely maintain a status file

Laszlo Nagy gandalf at shopzeus.com
Thu Jul 12 13:43:19 EDT 2012


>> This is not a contradiction. Although the rename operation is atomic,
>> the whole "change status" process is not. It is because there are two
>> operations: #1 delete old status file and #2. rename the new status
>> file. And because there are two operations, there is still a race
>> condition. I see no contradiction here.
> On Posix systems, you can avoid the race condition.  The trick is to
> skip step #1.  The rename will implicitly delete the old file, and
> it will still be atomic.  The whole process now consists of a single
> stop, so the whole process is now atomic.
Well, I didn't know that this is going to work. At least it does not 
work on Windows 7 (which should be POSIX compatible?)

 >>> f = open("test.txt","wb+")
 >>> f.close()
 >>> f2 = open("test2.txt","wb+")
 >>> f2.close()
 >>> import os
 >>> os.rename("test2.txt","test.txt")
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
WindowsError: [Error 183] File already exists
 >>>

I have also tried this on FreeBSD and it worked.

Now, let's go back to the original question:

>>>This works well on Linux but Windows raises an error when status_file already exists.

It SEEMS that the op wanted a solution for Windows....





More information about the Python-list mailing list