3
这是工作程序(我能听到在行动文本到语音):范围规则和线程
import pyttsx
import threading
def saythread(location, text):
engine = pyttsx.init()
engine.say(text)
engine.runAndWait()
e = (1, "please work, oh my god")
t = threading.Thread(target=saythread,args=e,name='sayitthread')
t.start()
如果程序改为
import pyttsx
import threading
def saythread(location, text):
global engine #(CHANGED) ADDED GLOBAL
engine.say(text)
engine.runAndWait()
e = (1, "please work, oh my god")
engine = pyttsx.init() #(CHANGED) ADDED VARIABLE
t = threading.Thread(target=saythread,args=e,name='sayitthread')
t.start()
然后获取'卡住'在行“engine.runAndWait()”,文本到语音不起作用。我猜测问题在于线程范围的规则。对? 基本上我想要的是..在我的主线程中引擎变量的'句柄'。所以我可以从主线程调用engine.stop()。
希望我是有道理的
感谢
谢谢你的时间。我试图将引擎作为参数传递,但没有奏效。使用锁定不起作用。但最后使用一个内部函数调用engine.stop()并使用队列告诉内部函数何时停止,终于解决了我的问题!非常感谢!! –
@ user161179 - 很高兴听到它!如果您只是为了触发单个事件而使用队列,则可以使用['Condition]](http://docs.python.org/library/threading.html#condition-objects)或['事件'](http://docs.python.org/library/threading.html#event-objects)对象。 – detly