运行以下程序时:
import multiprocessing
import pyttsx3
from multiprocessing import Process
class _TTS:
def __init__(self):
self.engine = pyttsx3.init('espeak')
self.engine.setProperty('rate', 175)
def start(self,text_):
self.engine.say(text_)
self.engine.runAndWait()
def Speakit(words):
print('running Speakit')
tts = _TTS()
tts.start(words)
del(tts)
def testing(n):
print(n)
if n == 0:
words = 'Argument is zero'
Speakit(words)
print(words)
else:
words = 'Argument is not zero'
Speakit(words)
print(words)
if __name__=="__main__":
words = 'start'
# Speakit(words)
p1=Process(target=testing,args=(0,))
p1.start()
p1.join()
p2=Process(target=testing,args=(5,))
p2.start()
p2.join()
print("We're done")
如果我主要评论掉Speakit,脚本运行正确,说明打印出的内容
Watson $ python3 mp2.py
0
running Speakit
Argument is zero
5
running Speakit
Argument is not zero
We're done
我基本上不评论Speakit,剧本只会说";启动";单词,然后不再说话,只是挂上
python3 mp2.py
running Speakit
0
running Speakit
不明白为什么
我已经对您的代码进行了快速旋转。起初,由于您的pyttsx引擎的初始化,解释器抛出了一些重大错误。我删除了'espeak'
参数,然后它确实起了作用,程序也确实说了这两行话。
可能使用try/except
进行故障排除。