File locking
Chuck Swiger
cswiger at mac.com
Mon May 5 21:27:48 EDT 2003
Jason Miller wrote:
[ ... ]
> Is there a portable (i.e. *nix, win32, mac) way to lock files in python?
> From what I know about these platforms it doesn't seem likely, but I
> figured I'd ask.
Alex Martelli provided a reference for Python code which invokes
underlying OS primitives like lockf() or fcntl(). Regardless of what
you do in your Python code, however, the underlying OS mechanisms for
locking don't always work well together: there exist platforms where
lockf(), fcntl(), and flock() don't know about each other's locks.
Sure, better platforms handle the interactions properly, but mixing
types of locking *will* bite you or someone else down the line. Oh,
yes, and you really don't want to try locking files on anything but a
local filesystem: rpc.lockd (used by NFS) and Samba's Windows-style
locking have had a history of frequent locking problems. [1]
Also note that if you are working with remote filesystems, or anything
having to do with email, Usenet news, or similar you should use explicit
.lock files as well. SysV Unices-- ie, Solaris-- wants you to use
something called maillock(). And Rich $alz of INN also wrote a nice
program called shlock in ~200 lines of C for doing locking for UUCP and
Usenet news. [2]
--
-Chuck
[1]: The Samba Team has done an amazing job and have been improving
Samba's locking quite steadily, and I don't especially want to criticise
their efforts. However, I wouldn't write a trading system or a medical
software system which tried to lock via a Samba-based fileshare.
(The less I say about rpc.lockd, the better. :-)
[2]: The important idea here is that your lockfile should contain the
PID of the process which holds the lock, so that another process trying
to acquire the lock can get a lock held by a process which died, or be
able to break the deadlock. This type of locking has been adapted by
system startup scripts which keep the PID of a process (like a Unix
daemon) under /var/run.
I need to start hacking Python somewhere, would a Python version of
shlock be of interest to anyone?
More information about the Python-list
mailing list