Function doesn't work the second time its called in a thread (Pyttsx module)
EDIT: DRASTICALLY reduced the code.
On python 2.7 , windows xp
Here is a small program, that uses the Pyttsx module to do some text to speech.
Working: speaks a particular string of text.A new thread ( runNewThread() ) is created which handles the speaking part.
In order to stop the speaking , I used a queue to communicate with the new thread and hence stop the Pyttsx engine.
This all works as expected. However after stopping the talking thread and then creating a new thread, then for some reason the engine doesn't speak at all(inside the 2nd new thread). The thread function is properly called, the variable type of the engine = Pyttsx.init()
is correct, other statements are executed correctly , its only the engine.runAndWait()
doesn't work. Why?
EDIT: IT ALSO throws an exception now Exception in thread sayitthread (most likely raised during interpreter shutdown):
import pyttsx
import threading
from threading import *
import datetime
import os
import sys
import time
from Queue import Queue
queue = Queue()
pauselocation = 0
wordsToSay = ''
play = 0
#CREATED NEW THREAD
def runNewThread(wordsToSay):
e = (queue, wordsToSay)
t = threading.Thread(target=saythread,args=e,name='sayitthread')
#t.daemon = True
t.start()
#FUNCTION USED AS TARGET FOR NEW THREAD
def saythread(queue , text):
global pauselocation
saythread.pause = 0
engine = pyttsx.init()
print ( type(engine) )
saythread.pausequeue1 = False
def onWord(name, location, length):
saythread.pausequeue1 = queue.get(False)
saythread.pause = location
if saythread.pausequeue1 == True :
print 'stopping engine'
engine.stop()
time.sleep(1)
def engineRun():
print 'engineRun' , type(engine)
engine.connect("started-word" , onWord )
print text
engine.say(text)
engine.runAndWait()
engineRun()
pauselocation = saythread.pause
if saythread.pausequeue1 == False:
print 'exiting from thread1'
os._exit(1)
print 'after everything'
if __name__ == '__main__':
wordsToSay = """However little known the feelings or views of such a man may be on his
first entering a neighbourhood, this truth is so well fixed in the minds
of the surrounding families, that he is considered the rightful property
of some one or other of their daughters."""
runNewThread(wordsToSay) #FIRST THREAD CREATED. THIS WORKS
time.sleep(5)
queue.put(True) #SEND IN MESSAGE TO STOP THE ENGINE AND THE THREAD
time.sleep(5)
runNewThread(wordsToSay) #START A NEW THREAD . THE THREAD STARTS. But the ENGINE WON"T WORK
EDIT: This is what my debugger says , just before running the second thread is created
t Thread: <Thread(Thread-4, stopped 1328)>
_Thread__block _Condition: <Condition(<thread.lock object at 0x00AD6420>, 0)>
_Condition__lock lock: <thread.l开发者_如何学JAVAock object at 0x00AD6420>
_Condition__waiters list: []
__len__ int: 0
_Verbose__verbose bool: False
_Thread__daemonic bool: False
_Thread__ident int: 1328
_Thread__initialized bool: True
_Thread__name str: Thread-4
_Thread__started _Event: <threading._Event object at 0x00E3FDD0>
_Thread__stderr file: <open file '<stderr>', mode 'w' at 0x00A650D0>
_Thread__stopped bool: True
_Verbose__verbose bool: False
additionalInfo PyDBAdditionalThreadInfoWithCurrentFramesSupport: State:1 Stop:None Cmd: None Kill:False
daemon bool: False
ident int: 1328
name str: Thread-4
IT seems to me that even though the thread is not alive ( t.isAlive() returns false ), the variable t is still there! me thinks I have to get rid of this t variable. I tried. gc.collect() , but it didn't help.
精彩评论