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 |
(...skipping 24 matching lines...) Expand all Loading... | |
35 | 35 |
36 # 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 |
37 # 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 |
38 # reference wouldn't update where the original reference pointed. | 38 # reference wouldn't update where the original reference pointed. |
39 ret = [None] | 39 ret = [None] |
40 def RunOnTimeoutThread(): | 40 def RunOnTimeoutThread(): |
41 ret[0] = func(*args, **kwargs) | 41 ret[0] = func(*args, **kwargs) |
42 | 42 |
43 while True: | 43 while True: |
44 try: | 44 try: |
45 name = 'TimeoutThread-for-%s' % threading.current_thread().name | 45 name = 'TimeoutThread-%d-for-%s' % (retries, |
jbudorick
2014/10/24 17:30:52
Good idea.
| |
46 threading.current_thread().name) | |
46 thread_group = reraiser_thread.ReraiserThreadGroup( | 47 thread_group = reraiser_thread.ReraiserThreadGroup( |
47 [TimeoutRetryThread(RunOnTimeoutThread, name=name)]) | 48 [TimeoutRetryThread(RunOnTimeoutThread, name=name)]) |
48 thread_group.StartAll() | 49 thread_group.StartAll() |
49 thread_group.JoinAll(watchdog_timer.WatchdogTimer(timeout)) | 50 thread_group.JoinAll(watchdog_timer.WatchdogTimer(timeout)) |
50 return ret[0] | 51 return ret[0] |
51 except: | 52 except: |
52 if retries <= 0: | 53 if retries <= 0: |
53 raise | 54 raise |
54 retries -= 1 | 55 retries -= 1 |
OLD | NEW |