134 | | try : |
135 | | while not haslock : |
136 | | self.logdebug("Trying to acquire lock %s" % self.lockfilename) |
137 | | try : |
138 | | self.lockfile = os.open(self.lockfilename, |
139 | | os.O_CREAT | os.O_EXCL | os.O_WRONLY, |
140 | | 0600) |
141 | | except OSError, error : |
142 | | # IMPORTANT : we don't check anymore if the other process |
143 | | # is still alive, otherwise we could inadvertantly delete |
144 | | # a lock file created by a different print server in the |
145 | | # same directory (load balancing). |
146 | | # I think the lock is removed in all cases anyway, excepted |
147 | | # when the admin kills -9 cupspykota. |
148 | | if error.errno == errno.EEXIST : |
149 | | self.logdebug("Lock not available. Waiting a bit...") |
150 | | else : |
151 | | self.logdebug("Lock not available : %s. Waiting a bit..." % error) |
152 | | time.sleep(0.25) # No hurry :) |
153 | | else : |
154 | | os.write(self.lockfile, str(self.pid)) |
155 | | haslock = True |
156 | | except IOError : |
157 | | self.logdebug("I/O Error while waiting for lock.") |
158 | | else : |
159 | | self.logdebug("Lock acquired.") |
| 134 | while not haslock : |
| 135 | try : |
| 136 | # open the lock file, optionally creating it if needed. |
| 137 | self.lockfile = open(self.lockfilename, "a+") |
| 138 | |
| 139 | # we wait indefinitely for the lock to become available. |
| 140 | # works over NFS too. |
| 141 | fcntl.lockf(self.lockfile, fcntl.LOCK_EX) |
| 142 | haslock = True |
| 143 | |
| 144 | self.logdebug("Lock %s acquired." % self.lockfilename) |
| 145 | |
| 146 | # Here we save the PID in the lock file, but we don't use |
| 147 | # it, because the lock file may be in a directory shared |
| 148 | # over NFS between two (or more) print servers, so the PID |
| 149 | # has no meaning in this case. |
| 150 | self.lockfile.truncate(0) |
| 151 | self.lockfile.seek(0, 0) |
| 152 | self.lockfile.write(str(self.pid)) |
| 153 | self.lockfile.flush() |
| 154 | except IOError : |
| 155 | self.logdebug("I/O Error while waiting for lock %s" % self.lockfilename) |
| 156 | time.sleep(0.25) |