added re-raise of exceptions and returning the result of func() to Task.wait()
This commit is contained in:
+20
-2
@@ -10,20 +10,31 @@ class Task(BaseTask):
|
||||
def __init__(self, func, stopper=None, hold=True):
|
||||
self.func = func
|
||||
self.stopper = stopper
|
||||
self.thread = Thread(target=func)
|
||||
self.thread = Thread(target=self.target)
|
||||
self.result = None
|
||||
self.exception = None
|
||||
if not hold:
|
||||
self.start()
|
||||
|
||||
def target(self):
|
||||
try:
|
||||
self.result = self.func()
|
||||
except BaseException as exc: # BaseException covers a few more cases than Exception
|
||||
self.exception = exc
|
||||
|
||||
def start(self):
|
||||
self.thread.start()
|
||||
|
||||
def stop(self):
|
||||
if self.stopper is not None:
|
||||
self.stopper()
|
||||
self.thread.join()
|
||||
return self.wait()
|
||||
|
||||
def wait(self):
|
||||
self.thread.join()
|
||||
if self.exception:
|
||||
raise TaskError from self.exception
|
||||
return self.result
|
||||
|
||||
@property
|
||||
def status(self):
|
||||
@@ -39,3 +50,10 @@ class Task(BaseTask):
|
||||
|
||||
|
||||
|
||||
class TaskError(RuntimeError):
|
||||
def __init__(self):
|
||||
message = "Exception in Task"
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user