Chromium Code Reviews| Index: build/android/pylib/utils/timeout_retry.py |
| diff --git a/build/android/pylib/utils/timeout_retry.py b/build/android/pylib/utils/timeout_retry.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7202f445deebeb8c20d4134fa6fe66a5602a49ac |
| --- /dev/null |
| +++ b/build/android/pylib/utils/timeout_retry.py |
| @@ -0,0 +1,42 @@ |
| +# Copyright 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""A class decorator that adds timeouts and retries to all public methods.""" |
|
frankf
2013/11/14 23:26:05
Update this
craigdh
2013/11/15 00:26:30
Done.
|
| + |
| +import functools |
| +import threading |
| + |
| +import reraiser_thread |
| +import watchdog_timer |
| + |
| + |
| +def Run(func, timeout, retries, args=[], kwargs={}): |
| + """Runs the passed function in a separate thread with timeouts and retries. |
|
frankf
2013/11/14 23:26:05
clarify that timeout applies to each try not total
craigdh
2013/11/15 00:26:30
Done.
|
| + |
| + Args: |
| + func: the function to be wrapped. |
| + timeout: the timeout in seconds. |
| + retries: the number of retries. |
| + args: list of positional args to pass to |func|. |
| + kwargs: dictionary of keyword args to pass to |func|. |
| + |
| + Returns: |
| + The return value of func(*args, **kwargs). |
| + """ |
| + ret = [None] |
|
frankf
2013/11/14 23:26:05
Why is this a list? Add a comment if there'a good
craigdh
2013/11/15 00:26:30
There's a good reason. Comment added.
|
| + def RunOnTimeoutThread(): |
| + ret[0] = func(*args, **kwargs) |
| + |
| + while True: |
| + try: |
| + retries -= 1 |
|
frankf
2013/11/14 23:26:05
you don't provide defaults for retries or timeout
frankf
2013/11/14 23:26:05
You have a off-by 1 error. retries=1 implies you t
craigdh
2013/11/15 00:26:30
Sure, I was considering it as tries but that behav
craigdh
2013/11/15 00:26:30
I wasn't intending to guess at defaults as those c
|
| + name = 'TimeoutThread-for-%s' % threading.current_thread().name |
| + thread_group = reraiser_thread.ReraiserThreadGroup( |
| + [reraiser_thread.ReraiserThread(RunOnTimeoutThread, name=name)]) |
| + thread_group.StartAll() |
| + thread_group.JoinAll(watchdog_timer.WatchdogTimer(timeout)) |
| + return ret[0] |
| + except: |
| + if retries <= 0: |
| + raise |