Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """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.
| |
| 6 | |
| 7 import functools | |
| 8 import threading | |
| 9 | |
| 10 import reraiser_thread | |
| 11 import watchdog_timer | |
| 12 | |
| 13 | |
| 14 def Run(func, timeout, retries, args=[], kwargs={}): | |
| 15 """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.
| |
| 16 | |
| 17 Args: | |
| 18 func: the function to be wrapped. | |
| 19 timeout: the timeout in seconds. | |
| 20 retries: the number of retries. | |
| 21 args: list of positional args to pass to |func|. | |
| 22 kwargs: dictionary of keyword args to pass to |func|. | |
| 23 | |
| 24 Returns: | |
| 25 The return value of func(*args, **kwargs). | |
| 26 """ | |
| 27 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.
| |
| 28 def RunOnTimeoutThread(): | |
| 29 ret[0] = func(*args, **kwargs) | |
| 30 | |
| 31 while True: | |
| 32 try: | |
| 33 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
| |
| 34 name = 'TimeoutThread-for-%s' % threading.current_thread().name | |
| 35 thread_group = reraiser_thread.ReraiserThreadGroup( | |
| 36 [reraiser_thread.ReraiserThread(RunOnTimeoutThread, name=name)]) | |
| 37 thread_group.StartAll() | |
| 38 thread_group.JoinAll(watchdog_timer.WatchdogTimer(timeout)) | |
| 39 return ret[0] | |
| 40 except: | |
| 41 if retries <= 0: | |
| 42 raise | |
| OLD | NEW |