| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """A utility to run functions with timeouts and retries.""" | 5 """A utility to run functions with timeouts and retries.""" |
| 6 # pylint: disable=W0702 | 6 # pylint: disable=W0702 |
| 7 | 7 |
| 8 import threading | 8 import threading |
| 9 | 9 |
| 10 from pylib.utils import reraiser_thread | 10 from pylib.utils import reraiser_thread |
| 11 from pylib.utils import watchdog_timer | 11 from pylib.utils import watchdog_timer |
| 12 | 12 |
| 13 | 13 |
| 14 class TimeoutRetryThread(reraiser_thread.ReraiserThread): |
| 15 pass |
| 16 |
| 17 |
| 14 def Run(func, timeout, retries, args=None, kwargs=None): | 18 def Run(func, timeout, retries, args=None, kwargs=None): |
| 15 """Runs the passed function in a separate thread with timeouts and retries. | 19 """Runs the passed function in a separate thread with timeouts and retries. |
| 16 | 20 |
| 17 Args: | 21 Args: |
| 18 func: the function to be wrapped. | 22 func: the function to be wrapped. |
| 19 timeout: the timeout in seconds for each try. | 23 timeout: the timeout in seconds for each try. |
| 20 retries: the number of retries. | 24 retries: the number of retries. |
| 21 args: list of positional args to pass to |func|. | 25 args: list of positional args to pass to |func|. |
| 22 kwargs: dictionary of keyword args to pass to |func|. | 26 kwargs: dictionary of keyword args to pass to |func|. |
| 23 | 27 |
| 24 Returns: | 28 Returns: |
| 25 The return value of func(*args, **kwargs). | 29 The return value of func(*args, **kwargs). |
| 26 """ | 30 """ |
| 27 if not args: | 31 if not args: |
| 28 args = [] | 32 args = [] |
| 29 if not kwargs: | 33 if not kwargs: |
| 30 kwargs = {} | 34 kwargs = {} |
| 31 | 35 |
| 32 # The return value uses a list because Python variables are references, not | 36 # The return value uses a list because Python variables are references, not |
| 33 # values. Closures make a copy of the reference, so updating the closure's | 37 # values. Closures make a copy of the reference, so updating the closure's |
| 34 # reference wouldn't update where the original reference pointed. | 38 # reference wouldn't update where the original reference pointed. |
| 35 ret = [None] | 39 ret = [None] |
| 36 def RunOnTimeoutThread(): | 40 def RunOnTimeoutThread(): |
| 37 ret[0] = func(*args, **kwargs) | 41 ret[0] = func(*args, **kwargs) |
| 38 | 42 |
| 39 while True: | 43 while True: |
| 40 try: | 44 try: |
| 41 name = 'TimeoutThread-for-%s' % threading.current_thread().name | 45 name = 'TimeoutThread-for-%s' % threading.current_thread().name |
| 42 thread_group = reraiser_thread.ReraiserThreadGroup( | 46 thread_group = reraiser_thread.ReraiserThreadGroup( |
| 43 [reraiser_thread.ReraiserThread(RunOnTimeoutThread, name=name)]) | 47 [TimeoutRetryThread(RunOnTimeoutThread, name=name)]) |
| 44 thread_group.StartAll() | 48 thread_group.StartAll() |
| 45 thread_group.JoinAll(watchdog_timer.WatchdogTimer(timeout)) | 49 thread_group.JoinAll(watchdog_timer.WatchdogTimer(timeout)) |
| 46 return ret[0] | 50 return ret[0] |
| 47 except: | 51 except: |
| 48 if retries <= 0: | 52 if retries <= 0: |
| 49 raise | 53 raise |
| 50 retries -= 1 | 54 retries -= 1 |
| OLD | NEW |