navigate.model.concurrency.concurrency_tools.ResultThread
- class navigate.model.concurrency.concurrency_tools.ResultThread(group=None, target=None, name=None, args=(), kwargs=None)
Bases:
Threadthreading.Thread with all the simple features we wish it had.
We added a ‘get_result’ method that returns values/raises exceptions.
We changed the return value of ‘start’ from ‘None’ to ‘self’ – just to trivially save us a line of code when launching threads.
Examples
def f(a): ''' A function that does something... ''' return a.sum() ## ## Getting Results: ## a = np.ones((2,), dtype='uint8') # Our problem: th = threading.Thread(target=f, args=(a,)) th.start() th.join() # We can't access the result of f(a) without redefining f! # Our solution: res_th = ResultThread(target=f, args=(a,)).start() res = res_th.get_result() # returns f(a) assert res == 2 ## ## Error handling ## a = 1 # Our problem: th = threading.Thread(target=f, args=(a,)) th.start() th.join() # f(a) raised an unhandled exception. Our parent thread has no idea! # Our solution: res_th = ResultThread(target=f, args=(a,)).start() try: res = res_th.get_result() except AttributeError: print("AttributeError was raised in thread!") else: raise AssertionError( 'We expected an AttributeError to be raised on join!') # Unhandled exceptions raised during evaluation of 'f' are reraised in # the parent thread when you call 'get_result'. # Tracebacks may print to STDERR when the exception occurs in # the child thread, but don't affect the parent thread (yet).
Notes
This module modifies threading.excepthook. You can’t just copy/paste this class definition and expect it to work.
- __init__(group=None, target=None, name=None, args=(), kwargs=None)
This constructor should always be called with keyword arguments. Arguments are:
group should be None; reserved for future extension when a ThreadGroup class is implemented.
target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.
args is a list or tuple of arguments for the target invocation. Defaults to ().
kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.
If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.
Methods
__init__([group, target, name, args, kwargs])This constructor should always be called with keyword arguments.
get_result([timeout])Either returns a value or raises an exception.
run()Method representing the thread's activity.
start()Start the thread's activity.
- get_result(timeout=None)
Either returns a value or raises an exception.
Optionally accepts a timeout in seconds. If thread has not returned after timeout seconds, raises a TimeoutError.
- run()
Method representing the thread’s activity.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
- start()
Start the thread’s activity.
It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.
This method will raise a RuntimeError if called more than once on the same thread object.