Package pythonutils :: Module pathutils :: Class LockFile
[hide private]
[frames] | no frames]

Class LockFile
source code

object --+    
         |    
      Lock --+
             |
            LockFile

A file like object with an exclusive lock, whilst it is open.

The lock is provided by the Lock class, which creates a directory with the same name as the file (plus a trailing underscore), to indicate that the file is locked.

This is simple and cross platform, with some limitations :



Instance Methods [hide private]
  __init__(self, filename, mode='r', bufsize=-1, timeout=5, step=0.1, force=True)
Create a file like object that is locked (using the Lock class) until it is closed.
  close(self, ignore=True)
close the file and release the lock.
  __getattr__(self, name)
delegate appropriate method/attribute calls to the file.
  __setattr__(self, name, value)
Only allow attribute setting that don't clash with the file.
  __del__(self)
Auto unlock (and close file) when object is deleted.

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __str__

    Inherited from Lock
  _mungedname(self)
Override this in a subclass if you want to change the way Lock creates the directory name.
  lock(self, force=True)
Lock the file for access by creating a directory of the same name (plus a trailing underscore).
  unlock(self, ignore=True)
Release the lock.

Class Variables [hide private]

Inherited from object: __class__


Method Details [hide private]

__init__(self, filename, mode='r', bufsize=-1, timeout=5, step=0.1, force=True)
(Constructor)

source code 

Create a file like object that is locked (using the Lock class) until it is closed.

The file is only locked against another process that attempts to acquire a lock using Lock (or LockFile).

The lock is released automatically when the file is closed.

The filename, mode and bufsize arguments have the same meaning as for the built in function open.

The timeout and step arguments have the same meaning as for a Lock object.

The force argument has the same meaning as for the Lock.lock method.

A LockFile object has all the normal file methods and attributes.

Overrides: Lock.__init__

close(self, ignore=True)

source code 

close the file and release the lock.

ignore has the same meaning as for Lock.unlock

__getattr__(self, name)
(Qualification operator)

source code 
delegate appropriate method/attribute calls to the file.

__setattr__(self, name, value)

source code 
Only allow attribute setting that don't clash with the file.
Overrides: object.__setattr__

__del__(self)
(Destructor)

source code 
Auto unlock (and close file) when object is deleted.
Overrides: Lock.__del__