From 4c2ec2aa6e86e2fbcdd6b0d8da46fab7811d430c Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Fri, 3 Jul 2020 11:20:55 +0200 Subject: [PATCH] made it possible to stop task.wait() via ctrl-c --- slic/core/task/task.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/slic/core/task/task.py b/slic/core/task/task.py index ddf3788c6..a990992e4 100644 --- a/slic/core/task/task.py +++ b/slic/core/task/task.py @@ -38,7 +38,13 @@ class Task(BaseTask): return self.wait() def wait(self): - self.thread.join() + try: + staggered_join(self.thread) + except KeyboardInterrupt: + print() # print new line after ^C + if self.stopper: + self.stopper() + self.thread.join() #TODO: should this timeout? if self.exception: raise TaskError from self.exception return self.result @@ -64,3 +70,12 @@ class TaskError(RuntimeError): +def staggered_join(thread, timeout=1): + """ + Continuously join for timeout seconds to not block KeyboardInterrupt + """ + while thread.is_alive(): + thread.join(timeout) + + +