From 1326ab5004c2dcc40a34ae4ee77873dbd047a20e Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Fri, 15 May 2020 10:34:16 +0000 Subject: [PATCH] added re-raise of exceptions and returning the result of func() to Task.wait() --- slic/core/task/task.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/slic/core/task/task.py b/slic/core/task/task.py index 730164010..ed898bdb6 100644 --- a/slic/core/task/task.py +++ b/slic/core/task/task.py @@ -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) + + +