Merge "Reduce duplication of code in handling multi-type blocking argument"
This commit is contained in:
commit
a5cd1c34c4
@ -74,10 +74,7 @@ class FileLock(locking.Lock):
|
||||
return self.acquired
|
||||
|
||||
def acquire(self, blocking=True):
|
||||
timeout = None
|
||||
if not isinstance(blocking, bool):
|
||||
timeout = float(blocking)
|
||||
blocking = True
|
||||
blocking, timeout = utils.convert_blocking(blocking)
|
||||
watch = timeutils.StopWatch(duration=timeout)
|
||||
watch.start()
|
||||
while True:
|
||||
|
@ -64,19 +64,15 @@ class IPCLock(locking.Lock):
|
||||
self._lock = None
|
||||
|
||||
def acquire(self, blocking=True):
|
||||
if (blocking is not True
|
||||
and sysv_ipc.SEMAPHORE_TIMEOUT_SUPPORTED is False):
|
||||
raise tooz.NotImplemented(
|
||||
"This system does not support semaphore timeout")
|
||||
# Convert blocking argument to a valid timeout value
|
||||
if blocking is True:
|
||||
timeout = None
|
||||
start_time = None
|
||||
elif blocking is False:
|
||||
if (blocking is not True and
|
||||
sysv_ipc.SEMAPHORE_TIMEOUT_SUPPORTED is False):
|
||||
raise tooz.NotImplemented("This system does not support"
|
||||
" semaphore timeouts")
|
||||
blocking, timeout = utils.convert_blocking(blocking)
|
||||
start_time = None
|
||||
if not blocking:
|
||||
timeout = 0
|
||||
start_time = None
|
||||
else:
|
||||
timeout = blocking
|
||||
elif blocking and timeout is not None:
|
||||
start_time = time.time()
|
||||
while True:
|
||||
try:
|
||||
|
@ -74,14 +74,10 @@ class RedisLock(locking.Lock):
|
||||
return owner_tok == lock_tok
|
||||
|
||||
def acquire(self, blocking=True):
|
||||
if blocking is True or blocking is False:
|
||||
blocking_timeout = None
|
||||
else:
|
||||
blocking_timeout = float(blocking)
|
||||
blocking = True
|
||||
blocking, timeout = utils.convert_blocking(blocking)
|
||||
with _translate_failures():
|
||||
self.acquired = self._lock.acquire(
|
||||
blocking=blocking, blocking_timeout=blocking_timeout)
|
||||
blocking=blocking, blocking_timeout=timeout)
|
||||
if self.acquired:
|
||||
self._coord._acquired_locks.add(self)
|
||||
return self.acquired
|
||||
|
@ -56,11 +56,7 @@ class ZooKeeperLock(locking.Lock):
|
||||
cause=e)
|
||||
|
||||
def acquire(self, blocking=True):
|
||||
if isinstance(blocking, bool):
|
||||
timeout = None
|
||||
else:
|
||||
blocking = True
|
||||
timeout = float(blocking)
|
||||
blocking, timeout = utils.convert_blocking(blocking)
|
||||
self.acquired = self._lock.acquire(blocking=blocking,
|
||||
timeout=timeout)
|
||||
return self.acquired
|
||||
|
@ -104,6 +104,15 @@ def safe_abs_path(rooted_at, *pieces):
|
||||
return path
|
||||
|
||||
|
||||
def convert_blocking(blocking):
|
||||
"""Converts a multi-type blocking variable into its derivatives."""
|
||||
timeout = None
|
||||
if not isinstance(blocking, bool):
|
||||
timeout = float(blocking)
|
||||
blocking = True
|
||||
return blocking, timeout
|
||||
|
||||
|
||||
def ensure_tree(path):
|
||||
"""Create a directory (and any ancestor directories required).
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user