How can one open a file for exclusive access?

Alex Martelli aleax at aleax.it
Mon Jan 14 15:04:24 EST 2002


Syver Enstad wrote:
        ...
>> A good example of such file-locking, cross-platform, is at:
>> 
>> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203
> 
> Hmm... I might be mistaken here Alex, but as far as I can remember
> this is advisory file locking, where one is using a file as a kind of

No, check 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/filesio_39h4.asp
(MS online docs for the LockFileEx Win32 API, used for the Windows
side of the quoted recipe) -- there's nothing advisory about it.

> know of is to use the win32file functions that allows you to specify
> this, ie win32file.CreateFile and so on.

Right, and LockFileEx, which the recipe is using, is exactly one of
those functions.  Surely you don't think (non-advisory) file locking
can only be changed by closing/reopening a file in the most
intricate OS since VMS...?

The Unix side of the recipe uses fcntl and thus the advisory vs
mandatory nature of the locking varies.  On Unix System-V for example
(SVID 3), if I recall correctly, a file's locks are mandatory if the file
has permission bit sgid set and group execution permission reset.
I believe Linux imitates that style (I could be wrong, though).


Alex




More information about the Python-list mailing list