I have a flask app, in that I have a button name start and a cancel button. I have a task to process. I am creating a new thread for it to run in background setting as daemon thread. since it is a long running task, I am trying to provide a cancel option for it.
Methods used as below:
- I tried using Event by checking is_set() in while loop and on cancel button calling set()
- I tried using signals by passing thread id (ident) and SIGTERM signal to pthread_kill()
for both methods when I click on cancel button, The task does not get cancel and executes completely.
class BackgroundWorker(Thread):
# event = Event()
def __init__(self, name: str, daemon: bool, a: str, b: str, c: int) -> None:
self.a: str = a
self.b: str = b
self.c: int = c
# self.event = Event()
super().__init__(name=name, daemon=daemon)
def run(self) -> None:
while True:
long_background_task(self.a, self.b, self.c)
# Creating object
task = BackgroundWorker("task", True, a, b, c)
task.start()
Is there any other way to terminate the running thread or what I am missing in the listed methods?