flock experience

Eric Brunel eric.brunel at pragmadev.com
Tue Jan 14 15:23:50 CET 2003

Todd Warner wrote:
> File-locking *can* be tricky. Word of caution: if the log-file resides
> on an NFS file-system, things can be... unreliable. We've had issues in
> the past with that. NFS, from what I understand, cannot guarantee when
> something is actually written to disk (note: not an NFS expert).

Did the issues you had involve only "regular" file locking via flock or 
fcntl, or file read/writes as well?

I'm quite interested because we had a similar problem, with an added 
complexity: the file system we used was exported to Unix workstations via 
NFS and to Windows PCs via samba. I tried to use the portalocker module as 
found in ActiveState's Python Cookbook ( 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203 ), but it 
didn't seem to work as expected: a lock set on Unix worked among Unix 
workstations, but not for Windows PCs, and Windows didn't seem to be able 
to lock anything on the shared disk.

So we ended up doing our own lock system, based on the (maybe wrong) idea 
that a directory creation on any disk, shared or not, is supposed to be 
atomic: either the directory doesn't exist, and the creation works, or the 
directory already exists, and the creation fails.

So here is a simplified version of the code for our locking system:

import os

class AlreadyLocked(Exception):

class FileLock:

  def __init__(self, fileName):
    self.__lockName = os.path.join(os.path.dirname(fileName),
                                   ".%s.lck" % os.path.dirname(fileName))
    except OsError:
      self.__lockName = None
      raise AlreadyLocked()

  def release(self):
    if self.__lockName is None: return

Until now, this method has been quite successful. But is it really secure, 
or may we get problems someday?
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

More information about the Python-list mailing list