923 | | stillrunning = True |
924 | | while 1 : |
925 | | self.LockFile = open(lockfilename, "a+") |
926 | | fcntl.lockf(self.LockFile, fcntl.LOCK_EX) # Will wait until lock available |
927 | | self.LockFile.seek(0, 0) |
928 | | try : |
929 | | oldpid = int(self.LockFile.readlines()[-1].strip()) |
930 | | except : |
931 | | stillrunning = False |
932 | | else : |
933 | | try : |
934 | | os.kill(oldpid, 0) |
935 | | except OSError : |
936 | | stillrunning = False |
937 | | if not stillrunning : |
| 922 | self.logDebug("Waiting for lock %s to become available..." % lockfilename) |
| 923 | haslock = False |
| 924 | while not haslock : |
| 925 | try : |
| 926 | # open the lock file, optionally creating it if needed. |
| 927 | self.LockFile = open(lockfilename, "a+") |
| 928 | |
| 929 | # we wait indefinitely for the lock to become available. |
| 930 | # works over NFS too. |
| 931 | fcntl.lockf(self.LockFile, fcntl.LOCK_EX) |
| 932 | haslock = True |
| 933 | |
| 934 | self.logDebug("Lock %s acquired." % lockfilename) |
| 935 | |
| 936 | # Here we save the PID in the lock file, but we don't use |
| 937 | # it, because the lock file may be in a directory shared |
| 938 | # over NFS between two (or more) print servers, so the PID |
| 939 | # has no meaning in this case. |
1270 | | self.logDebug("Removing lock...") |
1271 | | try : |
1272 | | fcntl.flock(self.LockFile, fcntl.LOCK_UN) |
1273 | | self.LockFile.close() |
1274 | | # NB : we don't remove the lock file, since it might already be opened by another process. |
1275 | | except : |
1276 | | self.logInfo("Problem while removing lock.", "error") |
1277 | | else : |
1278 | | self.logDebug("Lock removed.") |
| 1271 | |
| 1272 | if self.LockFile is not None : |
| 1273 | self.logDebug("Removing lock...") |
| 1274 | try : |
| 1275 | fcntl.lockf(self.LockFile, fcntl.LOCK_UN) |
| 1276 | self.LockFile.close() |
| 1277 | except : |
| 1278 | self.logInfo("Problem while unlocking.", "error") |
| 1279 | else : |
| 1280 | self.logDebug("Lock removed.") |