How to safely maintain a status file

rantingrickjohnson at gmail.com rantingrickjohnson at gmail.com
Fri Jul 13 00:26:20 EDT 2012


On Thursday, July 12, 2012 10:13:47 PM UTC-5, Steven D'Aprano wrote:
> Rick has obviously never tried to open a file for reading when somebody 
> else has it opened, also for reading, and discovered that despite Windows 
> being allegedly a multi-user operating system, you can't actually have 
> multiple users read the same files at the same time.

You misread my response. My comment was direct result of Christian stating:

(paraphrase) "On some systems you are not permitted to delete a file whilst the file is open "

...which seems to be consistent to me. Why would *anybody* want to delete a file whilst the file is open? Bringing back the car analogy again: Would you consider jumping from a moving vehicle a consistent interaction with the interface of a vehicle? Of course not. The interface for a vehicle is simple and consistent:

 1. You enter the vehicle at location A
 2. The vehicle transports you to location B
 3. You exit the vehicle

At no time during the trip would anyone expect you to leap from the vehicle. But when you delete open files, you are essentially leaping from the moving vehicle! This behavior goes against all expectations of consistency in an API -- and against all sanity when riding in a vehicle!

> Opening files for exclusive read *by default* is a pointless and silly 
> limitation. It's also unsafe: if a process opens a file for exclusive 
> read, and then dies, *no other process* can close that file.

Oh come on. Are you actually going to use "errors" or "unintended consequences", or even "Acts of God" to defend your argument? Okay. Okay. I suppose "IF" the car spontaneously combusted "THEN" the passengers would be wise to jump out, leaving the vehicle to the whims of inertia.

> One neat trick is to open a file, then delete it from disk while it is 
> still open. So long as your process is still running, you can write to 
> this ghost file, as normal, but no other process can (easily) see it. And 
> when your process ends, the file contents is automatically deleted.

Well "neat tricks" aside, I am of the firm belief that deleting files should never be possible whilst they are open. 

 * Opening files requires that data exist on disk
 * Reading and writing files requires an open file obj
 * Closing files requires an open file object
 * And deleting files requires that the file NOT be open

Would you also entertain the idea of reading or writing files that do not exist? (not including pseudo file objs like StringIO of course!).

Summary: Neat tricks and Easter eggs are real hoot, but consistency in APIs is the key.



More information about the Python-list mailing list