def acquire(self):
"""
Attempt to acquire the lock, returning a boolean that represents if the
lock is held.
"""
# NOTE: This isn't API compatible with the standard Python
# ``Lock.acquire`` method signature. It may make sense to make these
# compatible in the future, but that would also require changes to the
# the constructor: https://docs.python.org/2/library/threading.html#lock-objects
time_remaining = self.seconds_remaining
if time_remaining:
raise LockAlreadyHeld('Tried to acquire lock that is already held, %.3fs remaining: %r' % (time_remaining, self))
self.__acquired_at = None
delay = 0.01 + random.random() / 10
for i in xrange(int(self.timeout // delay)):
if i != 0:
sleep(delay)
attempt_started_at = time()
if self.cache.add(self.lock_key, '', self.timeout):
self.__acquired_at = attempt_started_at
break
if self.nowait:
break
return self.__acquired_at is not None
评论列表
文章目录