Exclusively lock a file to prevent other processes from reading it?

Nobody nobody at nowhere.com
Tue May 4 20:03:17 EDT 2010


On May 4, 2010, at 5:37 PM, python at bdurham.com wrote:

> Is there a way to exclusively lock a file to prevent other processes
> from reading it while we have it open?

> My environment is Python 2.6.4 (32-bit) under Windows, but I'm looking
> for a cross-platform solution if that's possible.

Some Unix systems support mandatory locking; Linux used to, but the option
appears to have been removed (and was seldom enabled).

On Unix systems which support mandatory locking, it is enabled for a
particular file by setting the setgid bit on a file which lacks
group-execute permission. This causes fcntl() locks to be mandatory, i.e.
they cause read()/write() operations to block rather than merely
preventing conflicting locks from being obtained.

But in general, you can't rely upon mandatory locking being available.




More information about the Python-list mailing list