Answer the question
In order to leave comments, you need to log in
How to stop a Thread?
The function invokes a thread (a progress bar for that function). How can I stop the thread when this function completes.
def main_func():
thread_indicator = threading.Thread(target=indicator_edit)
thread_indicator.start()
time.sleep(5) # do something
# thread.kill()
Answer the question
In order to leave comments, you need to log in
There is no native solution to this problem. Found this handy example .
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self, *args, **kwargs):
super(StoppableThread, self).__init__(*args, **kwargs)
self._stop_event = threading.Event()
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question