diff options
| author | Jenkins <jenkins@review.openstack.org> | 2015-02-18 05:02:45 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2015-02-18 05:02:45 +0000 |
| commit | 67905d59f6faf93aaa887416dc67416f237614a3 (patch) | |
| tree | b2ae4e7473ca76646c39494b6765afd3b74109bb /taskflow/utils | |
| parent | cae3bf108276859fa58433020219cc421e66b54c (diff) | |
| parent | 23c83e0f8e617a25cca31a88c6af9ab2f53e4439 (diff) | |
| download | taskflow-67905d59f6faf93aaa887416dc67416f237614a3.tar.gz | |
Merge "Improve multilock class and its associated unit test"
Diffstat (limited to 'taskflow/utils')
| -rw-r--r-- | taskflow/utils/lock_utils.py | 12 | ||||
| -rw-r--r-- | taskflow/utils/misc.py | 15 |
2 files changed, 20 insertions, 7 deletions
diff --git a/taskflow/utils/lock_utils.py b/taskflow/utils/lock_utils.py index ea2023e..1ee924b 100644 --- a/taskflow/utils/lock_utils.py +++ b/taskflow/utils/lock_utils.py @@ -363,21 +363,19 @@ class MultiLock(object): # Cleans off one level of the stack (this is done so that if there # are multiple __enter__() and __exit__() pairs active that this will # only remove one level (the last one), and not all levels... - leftover = self._lock_stacks[-1] - while leftover: - lock = self._locks[leftover - 1] + for left in misc.countdown_iter(self._lock_stacks[-1]): + lock_idx = left - 1 + lock = self._locks[lock_idx] try: lock.release() except (threading.ThreadError, RuntimeError) as e: # Ensure that we adjust the lock stack under failure so that # if release is attempted again that we do not try to release # the locks we already released... - self._lock_stacks[-1] = leftover + self._lock_stacks[-1] = left raise threading.ThreadError( "Unable to release lock %s/%s due to '%s'" - % (leftover, len(self._locks), e)) - else: - leftover -= 1 + % (left, len(self._locks), e)) # At the end only clear it off, so that under partial failure we don't # lose any locks... self._lock_stacks.pop() diff --git a/taskflow/utils/misc.py b/taskflow/utils/misc.py index 299082a..b2e967d 100644 --- a/taskflow/utils/misc.py +++ b/taskflow/utils/misc.py @@ -74,6 +74,21 @@ def find_monotonic(allow_time_time=False): return None +def countdown_iter(start_at, decr=1): + """Generator that decrements after each generation until <= zero. + + NOTE(harlowja): we can likely remove this when we can use an + ``itertools.count`` that takes a step (on py2.6 which we still support + that step parameter does **not** exist and therefore can't be used). + """ + if decr <= 0: + raise ValueError("Decrement value must be greater" + " than zero and not %s" % decr) + while start_at > 0: + yield start_at + start_at -= decr + + def merge_uri(uri, conf): """Merges a parsed uri into the given configuration dictionary. |
