Rev 5654: Use clearer names. in file:///home/vila/src/bzr/experimental/thread-with-exception/
Vincent Ladeuil
v.ladeuil+lp at free.fr
Tue Feb 8 16:26:23 UTC 2011
At file:///home/vila/src/bzr/experimental/thread-with-exception/
------------------------------------------------------------
revno: 5654
revision-id: v.ladeuil+lp at free.fr-20110208162623-2njflo8y7rrtek3n
parent: v.ladeuil+lp at free.fr-20110208155440-lns0pivfv8vskxji
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: thread-with-exception
timestamp: Tue 2011-02-08 17:26:23 +0100
message:
Use clearer names.
-------------- next part --------------
=== modified file 'bzrlib/tests/test_http.py'
--- a/bzrlib/tests/test_http.py 2011-02-08 15:54:40 +0000
+++ b/bzrlib/tests/test_http.py 2011-02-08 16:26:23 +0000
@@ -180,7 +180,7 @@
self.host, self.port = self._sock.getsockname()
self._ready = threading.Event()
self._thread = test_server.TestThread(
- event=self._ready, target=self._accept_read_and_reply)
+ sync_event=self._ready, target=self._accept_read_and_reply)
self._thread.start()
if 'threads' in tests.selftest_debug_flags:
sys.stderr.write('Thread started: %s\n' % (self._thread.ident,))
=== modified file 'bzrlib/tests/test_server.py'
--- a/bzrlib/tests/test_server.py 2011-02-08 15:54:40 +0000
+++ b/bzrlib/tests/test_server.py 2011-02-08 16:26:23 +0000
@@ -243,7 +243,7 @@
raise NotImplementedError
-class TestThread(thread.ThreadWithException):
+class TestThread(thread.CatchingExceptionThread):
def join(self, timeout=5):
"""Overrides to use a default timeout.
@@ -438,7 +438,7 @@
started = threading.Event()
stopped = threading.Event()
t = TestThread(
- event=stopped,
+ sync_event=stopped,
name='%s -> %s' % (client_address, self.server_address),
target = self.process_request_thread,
args = (started, stopped, request, client_address))
@@ -505,7 +505,7 @@
def start_server(self):
self.server = self.create_server()
self._server_thread = TestThread(
- event=self.server.started,
+ sync_event=self.server.started,
target=self.run_server)
self._server_thread.start()
# Wait for the server thread to start (i.e release the lock)
@@ -521,7 +521,7 @@
self._server_thread.pending_exception()
# From now on, we'll use a different event to ensure the server can set
# its exception
- self._server_thread.set_ready_event(self.server.stopped)
+ self._server_thread.set_sync_event(self.server.stopped)
def run_server(self):
self.server.serve()
=== modified file 'bzrlib/tests/test_test_server.py'
--- a/bzrlib/tests/test_test_server.py 2011-02-08 15:54:40 +0000
+++ b/bzrlib/tests/test_test_server.py 2011-02-08 16:26:23 +0000
@@ -181,7 +181,7 @@
def handle_connection(self):
req = self.rfile.readline()
- threading.currentThread().set_ready_event(sync)
+ threading.currentThread().set_sync_event(sync)
raise FailToRespond()
server = self.get_server(
@@ -203,7 +203,7 @@
def handle(self):
# We want to sync with the thread that is serving the
# connection.
- threading.currentThread().set_ready_event(sync)
+ threading.currentThread().set_sync_event(sync)
raise CantServe()
server = self.get_server(
=== modified file 'bzrlib/tests/test_thread.py'
--- a/bzrlib/tests/test_thread.py 2011-02-08 15:54:40 +0000
+++ b/bzrlib/tests/test_thread.py 2011-02-08 16:26:23 +0000
@@ -22,13 +22,13 @@
)
-class TestThreadWithException(tests.TestCase):
+class TestCatchingExceptionThread(tests.TestCase):
def test_start_and_join_smoke_test(self):
def do_nothing():
pass
- tt = thread.ThreadWithException(target=do_nothing)
+ tt = thread.CatchingExceptionThread(target=do_nothing)
tt.start()
tt.join()
@@ -39,7 +39,7 @@
def raise_my_exception():
raise MyException()
- tt = thread.ThreadWithException(target=raise_my_exception)
+ tt = thread.CatchingExceptionThread(target=raise_my_exception)
tt.start()
self.assertRaises(MyException, tt.join)
@@ -54,7 +54,7 @@
# Now we can raise
raise MyException()
- tt = thread.ThreadWithException(target=raise_my_exception)
+ tt = thread.CatchingExceptionThread(target=raise_my_exception)
tt.start()
tt.join(timeout=0)
self.assertIs(None, tt.exception)
=== modified file 'bzrlib/thread.py'
--- a/bzrlib/thread.py 2011-02-08 15:54:40 +0000
+++ b/bzrlib/thread.py 2011-02-08 16:26:23 +0000
@@ -18,8 +18,8 @@
import threading
-class ThreadWithException(threading.Thread):
- """A catching exception thread.
+class CatchingExceptionThread(threading.Thread):
+ """A thread that keeps track of exceptions.
If an exception occurs during the thread execution, it's caught and
re-raised when the thread is joined().
@@ -31,12 +31,12 @@
# blocked. The main example is a calling thread that want to wait for
# the called thread to be in a given state before continuing.
try:
- event = kwargs.pop('event')
+ sync_event = kwargs.pop('sync_event')
except KeyError:
# If the caller didn't pass a specific event, create our own
- event = threading.Event()
- super(ThreadWithException, self).__init__(*args, **kwargs)
- self.set_ready_event(event)
+ sync_event = threading.Event()
+ super(CatchingExceptionThread, self).__init__(*args, **kwargs)
+ self.set_sync_event(sync_event)
self.exception = None
self.ignored_exceptions = None # see set_ignored_exceptions
@@ -44,8 +44,8 @@
if sys.version_info < (2, 6):
name = property(threading.Thread.getName, threading.Thread.setName)
- def set_ready_event(self, event):
- """Set the ``ready`` event used to synchronize exception catching.
+ def set_sync_event(self, event):
+ """Set the ``sync_event`` event used to synchronize exception catching.
When the thread uses an event to synchronize itself with another thread
(setting it when the other thread can wake up from a ``wait`` call),
@@ -54,8 +54,11 @@
Some threads require multiple events and should set the relevant one
when appropriate.
+
+ Note that the event should be cleared so the caller can wait() on him
+ and be released when the thread set the event.
"""
- self.ready = event
+ self.sync_event = event
def set_ignored_exceptions(self, ignored):
"""Declare which exceptions will be ignored.
@@ -77,28 +80,24 @@
def run(self):
"""Overrides Thread.run to capture any exception."""
- self.ready.clear()
+ self.sync_event.clear()
try:
try:
- super(ThreadWithException, self).run()
+ super(CatchingExceptionThread, self).run()
except:
self.exception = sys.exc_info()
finally:
# Make sure the calling thread is released
- self.ready.set()
-
-
- def join(self, timeout=5):
+ self.sync_event.set()
+
+
+ def join(self, timeout=None):
"""Overrides Thread.join to raise any exception caught.
-
Calling join(timeout=0) will raise the caught exception or return None
if the thread is still alive.
-
- The default timeout is set to 5 and should expire only when a thread
- serving a client connection is hung.
"""
- super(ThreadWithException, self).join(timeout)
+ super(CatchingExceptionThread, self).join(timeout)
if self.exception is not None:
exc_class, exc_value, exc_tb = self.exception
self.exception = None # The exception should be raised only once
More information about the bazaar-commits
mailing list