Package concurrent_tree_crawler :: Package common :: Package threads :: Module ex_thread
[hide private]
[frames] | no frames]

Source Code for Module concurrent_tree_crawler.common.threads.ex_thread

 1  import sys 
 2  import threading 
 3  import Queue 
 4   
5 -class ExThread(threading.Thread):
6 """A thread that can throw an exception to the joining thread.""" 7
8 - def __init__(self, status_queue = None):
9 """ 10 @param status_queue: a queue that will hold an exception if it is 11 thrown by the thread 12 @type status_queue: L{Queue.Queue} or C{None} 13 """ 14 threading.Thread.__init__(self) 15 self.__status_queue = status_queue 16 if self.__status_queue is None: 17 self.__status_queue = Queue.Queue()
18
19 - def run_with_exception(self):
20 """This method should be overriden.""" 21 raise NotImplementedError
22
23 - def run(self):
24 """DO NOT override this method.""" 25 try: 26 self.run_with_exception() 27 except Exception: 28 self.__status_queue.put(sys.exc_info()) 29 self.__status_queue.put(None)
30
31 - def wait_for_exc_info(self):
32 return self.__status_queue.get()
33
34 - def join_with_exception(self):
35 ex_info = self.wait_for_exc_info() 36 if ex_info is None: 37 return 38 else: 39 raise ex_info[1]
40