[Python-Dev] Does Python need a file locking module (slightly higher level)?

skip at pobox.com skip at pobox.com
Tue Oct 23 05:30:58 CEST 2007


    >> I'm always daunted by the prospect of trying to implement file
    >> locking.

    Barry> If you want something like this, you might start by looking at
    Barry> Mailman's LockFile.py.

If I interpret the Python documentation for os.link correctly, the scheme
used by Mailman won't work on Windows (os.link isn't advertised as being
available there).  Nevertheless, the pointer to the Linux open(2) man page
was a good start.  Implementing something for Unix-y systems is not too
difficult using that advice.

Jean-Paul Calderone sent me a pointer to Twisted's file locking module.  I'm
still trying to figure out exactly what it does on Windows.  Something about
making and populating directories.  (os.mkdir is the replacement for
os.link?)

Benji York referred me to zc.lockfile.  That appears to use fcntl.flock.
Based on Jean-Paul's response it seems the jury is still out on whether
fcntl.flock works over NFS.  zc.lockfile at least has something specifically
for Windows.  Whether or not msvcrt.locking() works on networked file
systems remains to be seen.

Mailman's lockfile makes provision to block for a user-specified period of
time.  The other's push the waiting back onto the calling code.

It's not clear that any of these implementations is going to be perfect.
Maybe none ever will be.  In his reply Jean-Paul made this comment:

    It might be nice to have something like that in the standard library,
    but it's very simple once you know what to do.

I'm not so sure about the "very simple" part, especially if you aren't
familiar with all the ins and outs of the different platforms.  The fact
that the first three bits of code I was referred to were implemented by
three significant Python tools/platforms and that all are different in some
significant ways suggests that there is some both an underlying need for a
file locking mechanism but with a lack of consensus about the best way to
implement the mother-of-all-file-locking schemes for Python.  Maybe the best
place for this is in the distribution.  PEP?

Skip


More information about the Python-Dev mailing list