Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(432)

Side by Side Diff: build/android/pylib/utils/timeout_retry.py

Issue 132463007: Enable presubmit pylint in build/android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 6
7 import functools
8 import threading 7 import threading
9 8
10 import reraiser_thread 9 from . import reraiser_thread
11 import watchdog_timer 10 from . import watchdog_timer
12 11
13 12
14 def Run(func, timeout, retries, args=[], kwargs={}): 13 def Run(func, timeout, retries, args=None, kwargs=None):
15 """Runs the passed function in a separate thread with timeouts and retries. 14 """Runs the passed function in a separate thread with timeouts and retries.
16 15
17 Args: 16 Args:
18 func: the function to be wrapped. 17 func: the function to be wrapped.
19 timeout: the timeout in seconds for each try. 18 timeout: the timeout in seconds for each try.
20 retries: the number of retries. 19 retries: the number of retries.
21 args: list of positional args to pass to |func|. 20 args: list of positional args to pass to |func|.
22 kwargs: dictionary of keyword args to pass to |func|. 21 kwargs: dictionary of keyword args to pass to |func|.
23 22
24 Returns: 23 Returns:
25 The return value of func(*args, **kwargs). 24 The return value of func(*args, **kwargs).
26 """ 25 """
26 if not args:
27 args = []
28 if not kwargs:
29 kwargs = {}
30
27 # The return value uses a list because Python variables are references, not 31 # The return value uses a list because Python variables are references, not
28 # values. Closures make a copy of the reference, so updating the closure's 32 # values. Closures make a copy of the reference, so updating the closure's
29 # reference wouldn't update where the original reference pointed. 33 # reference wouldn't update where the original reference pointed.
30 ret = [None] 34 ret = [None]
31 def RunOnTimeoutThread(): 35 def RunOnTimeoutThread():
32 ret[0] = func(*args, **kwargs) 36 ret[0] = func(*args, **kwargs)
33 37
34 while True: 38 while True:
35 try: 39 try:
36 name = 'TimeoutThread-for-%s' % threading.current_thread().name 40 name = 'TimeoutThread-for-%s' % threading.current_thread().name
37 thread_group = reraiser_thread.ReraiserThreadGroup( 41 thread_group = reraiser_thread.ReraiserThreadGroup(
38 [reraiser_thread.ReraiserThread(RunOnTimeoutThread, name=name)]) 42 [reraiser_thread.ReraiserThread(RunOnTimeoutThread, name=name)])
39 thread_group.StartAll() 43 thread_group.StartAll()
40 thread_group.JoinAll(watchdog_timer.WatchdogTimer(timeout)) 44 thread_group.JoinAll(watchdog_timer.WatchdogTimer(timeout))
41 return ret[0] 45 return ret[0]
42 except: 46 except:
43 if retries <= 0: 47 if retries <= 0:
44 raise 48 raise
45 retries -= 1 49 retries -= 1
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698