MCPcopy Create free account
hub / github.com/ActiveState/code / acquire

Method acquire

recipes/Python/577524_Python_Sessions/recipe-577524.py:238–259  ·  view source on GitHub ↗

Acquire the lock, if possible. If the lock is in use, it check again every `wait` seconds. It does this until it either gets the lock or exceeds `timeout` number of seconds, in which case it throws an exception.

(self)

Source from the content-addressed store, hash-verified

236 self.delay = delay
237
238 def acquire(self):
239 """ Acquire the lock, if possible. If the lock is in use, it check again
240 every `wait` seconds. It does this until it either gets the lock or
241 exceeds `timeout` number of seconds, in which case it throws
242 an exception.
243 """
244 if self.is_locked:
245 return
246
247 start_time = time.time()
248 while True:
249 try:
250 self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR)
251 break;
252 except OSError as e:
253 if e.errno != errno.EEXIST:
254 raise
255
256 if (time.time() - start_time) >= self.timeout:
257 raise FileLockException("Timeout occured.")
258 time.sleep(self.delay)
259 self.is_locked = True
260
261 def release(self):
262 """ Get rid of the lock by deleting the lockfile.

Callers 15

__enter__Method · 0.95
acquireReadMethod · 0.45
acquireWriteMethod · 0.45
releaseMethod · 0.45
setThreadCountMethod · 0.45
getThreadCountMethod · 0.45
queueTaskMethod · 0.45
getNextTaskMethod · 0.45
joinAllMethod · 0.45
__init__Method · 0.45
__fetchMethod · 0.45
__getattribute__Method · 0.45

Calls 4

FileLockExceptionClass · 0.85
timeMethod · 0.45
openMethod · 0.45
sleepMethod · 0.45

Tested by

no test coverage detected