| # Gaudi Framework, version v22r0 |

Home |  | Generated: 9 Feb 2011

# locker Namespace Reference

## Classes
class | LockFile
| Lock a file. More...
## Functions
def | lock
def | unlock
def | _tmpFileName
def | _lckFileName
def | _linkCount
def | _read
def | _sleep
## Variables
| LOCK_EX = fcntl.F_WRLCK
| LOCK_SH = fcntl.F_RDLCK
| LOCK_NB = fcntl.F_UNLCK

## Function Documentation

def locker::_lckFileName | ( |  | fileName | ) | `[private]`

Definition at line 51 of file locker.py.

    00052                               :
    00053         return "%s.lock" % fileName

def locker::_linkCount | ( |  | lockFileName | ) | `[private]`

Definition at line 54 of file locker.py.

    00055                                   :
    00056         try:
    00057             return os.stat(lockFileName).st_nlink
    00058         except OSError, e:
    00059             if e.errno != errno.ENOENT:
    00060                 raise
                return -1

def locker::_read | ( |  | fileName | ) | `[private]`

Definition at line 61 of file locker.py.

    00062                        :
    00063         try:
    00064             fp = open(fileName)
    00065             try:     readFileName = fp.read()
    00066             finally: fp.close()
    00067             return readFileName
    00068         except EnvironmentError, e:
    00069             if e.errno != errno.ENOENT:
    00070                 raise
    00071             return None

def locker::_sleep | ( |  | ) | `[private]`

Definition at line 72 of file locker.py.

    00072                 : time.sleep(8)
    00073

def locker::_tmpFileName | ( |  | fileName | ) | `[private]`

Definition at line 49 of file locker.py.

    00050                               :
            return "%s.%s.%d" % ( fileName, socket.gethostname(), os.getpid() )

def locker::lock | ( |  | file | ) |

    Lock first 10 bytes of a file.

Definition at line 74 of file locker.py.

    00075                   :
    00076         fileName = file.name
    00077         tmpFileName = _tmpFileName(fileName)
    00078         fp = open( tmpFileName, "w" )
    00079         fp.write( tmpFileName )
    00080         fp.close()
    00081
    00082         lockFileName = _lckFileName( fileName )
    00083         while True:
    00084             if _linkCount(lockFileName) != -1: _sleep()
    00085             try:
    00086                 os.link( tmpFileName, lockFileName )
    00087                 # we acquired the lock
    00088                 return
    00089             except OSError, e:
    00090                 if e.errno == errno.ENOENT:
    00091                     pass
    00092                 elif e.errno != errno.EEXIST:
    00093                     os.unlink(tmpFileName)
    00094                     raise
    00095                 elif _linkCount( lockFileName ) != 2:
    00096                     pass
    00097                 elif _read(lockFileName) == tmpFileName:
    00098                     raise
    00099                 else:
    00100                     # someone else owns that lock
    00101                     pass
    00102             ## didn't get the lock !!
    00103             ## say something ?
    00104             #print _id_()," failed to acquire the lock ..."
    00105             _sleep()
    00106             pass
    00107         return

def locker::unlock | ( |  | file | ) |

    Unlock first 10 bytes of a file.

Definition at line 108 of file locker.py.

    00109                     :
    00110         fileName = file.name
    00111         tmpFileName  = _tmpFileName(fileName)
    00112         lockFileName = _lckFileName( fileName )
    00113
    00114         try:
    00115             os.unlink( lockFileName )
    00116         except OSError, e:
    00117             if e.errno != errno.ENOENT:
    00118                 raise
    00119         # remove the tmp file
    00120         try:
    00121             os.unlink( tmpFileName )
    00122         except OSError, e:
    00123             if e.errno != errno.ENOENT:
    00124                 raise
    00125         return

## Variable Documentation

locker::LOCK_EX = fcntl.F_WRLCK

Definition at line 12 of file locker.py.

locker::LOCK_NB = fcntl.F_UNLCK

Definition at line 14 of file locker.py.

locker::LOCK_SH = fcntl.F_RDLCK

Definition at line 13 of file locker.py.

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines