Is there a way in python to stop a thread? I have a gui with a method which plays 10 second audio files and sends information to GUI window continuously
I am Multithreading because I dont want the GUI to freeze while my files play. I can stop the thread with my current code but takes a while
My code looks something like this :
class MyThread(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(MyThread, self).__init__(*args, **kwargs)
self._stop = threading.Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
class Myplayer:
// GUI CODE
def play_button(self, widget):
self.mythread = MyThread(target=self.practice)
self.mythread.start()
def stop_button(self, widget):
if self.mythead.IsAlive:
self.self.stop()
def mplayer(self):
while not self.mythread.stopped:
gobject.idle_add(self.insert_text, "\nPlaying a new file")
subprocess.call(["timidity", "myfile.mid"])