How can one open a file for exclusive access?

Alex Martelli aleax at aleax.it
Tue Jan 15 08:57:48 EST 2002


"Parzival Herzog" <parz at shaw.SpamBucket.ca> wrote in message
news:QRH08.82207$pP5.9625048 at news1.rdc1.mb.home.com...
    ...
> Now, it seems that non-shared file access is the Win32 default,
> so can anyone explain why the Python "open", (which presumably
> is based on the C library "open", which on Win32 is based on
> "CreateFile", which defaults to non-shared access, ) gives me

It's somewhat of a stretch to interpret an argument of 0 (whose
meaning is not documented) as "defaulting".

> unrestricted shared access? Who would want such a thing by
> default?

That's what C programmers are used to, perhaps in part because:

> The argument between Alex and Syver suggests that in some or
> is it all Unices, unrestricted shared access is the norm. Is that so?

the open() system call, per se, does no locking on Unix.


> It would seem that writing a robust portable program (i.e. it can
> survive running a second instance of itself) for a simple
> matter such as updating a file would be VERY difficult in Python.

Not all that hard, depending on the kind of updates.  A typical
approach is to prepare the updated-file at leisure then move
it in place of the previous version atomically -- better than
keeping a lock arbitrarily long.


> BTW, if I were to use the file-locking  recipie Alex suggests,
> then is it true (under Windows) that if the lock is not explicitly
> released by the locking application, that it persists after the
> application terminates?

No.


Alex






More information about the Python-list mailing list