You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pyttsx3 runAndWait函数致代码终止、内核崩溃问题求助

解决pyttsx3连续调用runAndWait后内核崩溃的问题

针对你遇到的播放完第一段语音后代码终止、内核崩溃的问题,可尝试以下几种解决方案:

  • 合并语音请求,单次调用runAndWait
    pyttsx3的runAndWait()会阻塞直到所有语音播放完成,多次调用可能引发资源释放异常。可以将两段文本的语音请求合并后仅调用一次runAndWait():

    import pyttsx3
    engine = pyttsx3.init()
    engine.say("Hello, this is a sample text to speech conversion.")
    engine.say("This is a test to see if the second code works.")
    engine.runAndWait()
    engine.stop()
    
  • 每次播放后销毁并重建引擎实例
    如果必须分开播放两段语音,每次播放完成后显式停止引擎并销毁实例,再重新初始化:

    import pyttsx3
    
    # 播放第一段
    engine = pyttsx3.init()
    engine.say("Hello, this is a sample text to speech conversion.")
    engine.runAndWait()
    engine.stop()
    del engine
    
    # 播放第二段
    engine = pyttsx3.init()
    engine.say("This is a test to see if the second code works.")
    engine.runAndWait()
    engine.stop()
    del engine
    
  • 切换到普通Python脚本环境运行
    如果你是在Jupyter Notebook/Lab等交互式内核环境中运行代码,pyttsx3的后台线程可能与内核存在兼容性问题。尝试将代码保存为.py文件,用Python直接运行,大概率能解决内核崩溃问题。

  • 调整引擎属性避免资源冲突
    偶尔调整引擎的语速、音量等属性,也能规避部分资源占用异常:

    import pyttsx3
    engine = pyttsx3.init()
    engine.setProperty('rate', 150)  # 设置语速为150词/分钟
    engine.say("Hello, this is a sample text to speech conversion.")
    engine.runAndWait()
    engine.say("This is a test to see if the second code works.")
    engine.runAndWait()
    engine.stop()
    

内容的提问来源于stack exchange,提问作者David Lee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 09:57:36