函数在第二次调用时不起作用(Pyttsx模块)

时间:2011-07-21 07:33:33

标签: python

编辑:DRASTICALLY减少了代码。

在python 2.7上,windows xp

这是一个小程序,它使用Pyttsx模块进行一些文本到语音。

工作: 说出一个特定的文本字符串。创建一个处理说话部分的新线程(runNewThread())。

为了停止说话,我使用队列与新线程进行通信,从而停止Pyttsx引擎。

这一切都按预期工作。然而,在停止谈话线程然后创建新线程之后,由于某种原因,引擎根本不会说话(在第二个新线程内)。正确调用线程函数,engine = Pyttsx.init()的变量类型是正确的,其他语句正确执行,只有engine.runAndWait()不起作用。为什么呢?

编辑:它现在抛出异常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

编辑: 这是我的调试器所说的,就在创建第二个线程之前

t   Thread: <Thread(Thread-4, stopped 1328)>    
    _Thread__block  _Condition: <Condition(<thread.lock object at 0x00AD6420>, 0)>  
        _Condition__lock    lock: <thread.lock 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   

在我看来,即使线程不活动(t.isAlive()返回false),变量t仍然存在!我认为我必须摆脱这个变量。我试过了。 gc.collect(),但它没有帮助。

0 个答案:

没有答案