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

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

Issue 12317059: [Andoid] Threaded TestRunner creation and SetUp and TearDown calls. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 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 | Annotate | Revision Log
OLDNEW
(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 """Thread and ThreadGroup that reraise exceptions on the main thread."""
6
7 import sys
8 import threading
9
10
11 class ReraiserThread(threading.Thread):
12 """Thread class that can reraise exceptions."""
13 def __init__(self, func, args=[], kwargs={}):
14 super(ReraiserThread, self).__init__()
15 self.daemon = True
16 self._func = func
17 self._args = args
18 self._kwargs = kwargs
19 self._exc_info = None
20
21 def ReraiseIfException(self):
22 """Reraise exception if an exception was raised in the thread."""
23 if self._exc_info:
24 raise self._exc_info[0], self._exc_info[1], self._exc_info[2]
25
26 #override
27 def run(self):
28 """Overrides Thread.run() to add support for reraising exceptions."""
29 try:
30 self._func(*self._args, **self._kwargs)
31 except:
32 self._exc_info = sys.exc_info()
33 raise
34
35
36 class ReraiserThreadGroup(object):
37 """A group of ReraiserThread objects."""
38 def __init__(self, threads=[]):
39 """Initialize thread group.
40
41 Args:
42 threads: a list of ReraiserThread objects; defaults to empty.
43 """
44 self._threads = threads
45
46 def Add(self, thread):
47 """Add a thread to the group.
48
49 Args:
50 thread: a ReraiserThread object.
51 """
52 self._threads.append(thread)
53
54 def StartAll(self):
55 """Start all threads."""
56 for thread in self._threads:
57 thread.start()
58
59 def JoinAll(self):
60 """Join all threads.
61
62 Reraises exceptions raised by the child threads and supports
63 breaking immediately on exceptions raised on the main thread.
64 """
65 alive_threads = self._threads[:]
66 while alive_threads:
67 for thread in alive_threads[:]:
68 # Allow the main thread to periodically check for interrupts.
69 thread.join(0.1)
70 if not thread.isAlive():
71 alive_threads.remove(thread)
72 # All threads are allowed to complete before reraising exceptions.
73 for thread in self._threads:
74 thread.ReraiseIfException()
OLDNEW
« no previous file with comments | « build/android/pylib/gtest/test_runner.py ('k') | build/android/pylib/utils/reraiser_thread_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698