How to safely maintain a status file

Laszlo Nagy gandalf at shopzeus.com
Thu Jul 12 13:46:17 EDT 2012


>> Windows doesn't suppport atomic renames if the right side exists.  I
>> suggest that you implement two code paths:
>>
>> if os.name == "posix":
>>      rename = os.rename
>> else:
>>      def rename(a, b):
>>          try:
>>              os.rename(a, b)
>>          except OSError, e:
>>              if e.errno != 183:
>>                  raise
>>              os.unlink(b)
>>              os.rename(a, b)
>
> Problem is if the process is stopped between unlink and rename there
> would no status file.
Yes, and actually it does not need to be an abnormal termination. It is 
enough if the OS scheduler puts this process on hold for some time...

But using a lock file, the problem can be solved. However in that case, 
reading a status file can be a blocking operation.



More information about the Python-list mailing list