Reputation: 101
I have a function text_to_speach
that receives a text and speak it with gtts
module:
def text_to_speach(text):
try:
tts = gTTS(text=text, lang='fr', tld="com", slow=False)
tts.save("audio.mp3")
playsound.playsound('audio.mp3', False)
os.remove('audio.mp3')
except:
print('Check Speak issue !!!')
That function runs inside a Thread:
def speak(reply):
thread = Thread(target=text_to_speach, args=(reply,), daemon=True)
thread.start()
Now every time I run the speak()
function it creates a Thread
and I don't want it to create multiple Threads.
So I want every time I run speak
function the Thread will end after that.
Example:
speak("some text")
#Thread end
speak("some text 2")
#Thread end
speak("some text 3")
#Thread end
So my question is how to end the thread?
Upvotes: 0
Views: 62
Reputation: 987
Blocking solution:
def speak(reply):
thread = Thread(target=text_to_speach, args=(reply,), daemon = True)
thread.start()
thread.join() # it will block till thread ends
Upvotes: 2