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

Unified Diff: build/android/pylib/utils/timeout_retry.py

Issue 60043003: [android] Add timeout_retry module to pylib/utils/. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments Created 7 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « build/android/pylib/cmd_helper.py ('k') | build/android/pylib/utils/timeout_retry_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..cdd8b9a593bf5943f748b259b44b4dd3ba267806
--- /dev/null
+++ b/build/android/pylib/utils/timeout_retry.py
@@ -0,0 +1,45 @@
+# 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 utility to run functions with timeouts and retries."""
+
+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.
+
+ Args:
+ func: the function to be wrapped.
+ timeout: the timeout in seconds for each try.
+ 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).
+ """
+ # The return value uses a list because Python variables are references, not
+ # values. Closures make a copy of the reference, so updating the closure's
+ # reference wouldn't update where the original reference pointed.
+ ret = [None]
+ def RunOnTimeoutThread():
+ ret[0] = func(*args, **kwargs)
+
+ while True:
+ try:
+ 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
+ retries -= 1
« no previous file with comments | « build/android/pylib/cmd_helper.py ('k') | build/android/pylib/utils/timeout_retry_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698