made it possible to stop task.wait() via ctrl-c

This commit is contained in:
2020-07-03 11:20:55 +02:00
parent fb2f2afe53
commit 4c2ec2aa6e
+16 -1
View File
@@ -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)