How to use pyttsx in a python thread

See original GitHub issue

I am working with pyttsx3 for text-to-speech. I realized that I can’t use it within a thread (or I am doing something wrong). Do you know why?

Code Example:

from threading import Thread
import pyttsx3

def myfunc():
  engine = pyttsx3.init()
  engine.say("ok")
  engine.runAndWait()

t = Thread(target=myfunc)
t.start()

Error:

 File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python3/3.6.4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "test.py", line 9, in myfunc
    engine.runAndWait() #blocks
  File "/usr/local/lib/python3.6/site-packages/pyttsx3/engine.py", line 188, in runAndWait
    self.proxy.runAndWait()
  File "/usr/local/lib/python3.6/site-packages/pyttsx3/driver.py", line 204, in runAndWait
    self._driver.startLoop()
  File "/usr/local/lib/python3.6/site-packages/pyttsx3/drivers/nsss.py", line 33, in startLoop
    AppHelper.runConsoleEventLoop()
  File "/usr/local/lib/python3.6/site-packages/PyObjCTools/AppHelper.py", line 241, in runConsoleEventLoop
    nextfire = nextfire.earlierDate_(soon)
AttributeError: 'NoneType' object has no attribute 'earlierDate_'

As shown from stack-overflow replies, seem an OSX problem. Can you help me?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

21reactions
SqrtPaperecommented, Feb 16, 2018

Thanks for your interest @Kerang !

I already worked around the problem using this bit of code in thread run method:

from subprocess import call
phrase = "Hi everyone"
call(["python3", "speak.py", phrase])

where speak.py is:

import sys
import pyttsx3

def init_engine():
    engine = pyttsx3.init()
    return engine

def say(s):
    engine.say(s)
    engine.runAndWait() #blocks

engine = init_engine()
say(str(sys.argv[1]))

I never posted this because I thought it wasn’t a solution, but just a work around. Frankly I was waiting for the developers of pyttsx to resolve this problem. I think pyttsx may be the best text-to-speech engine around at the moment and not being able to use it in a OSX thread is a real drawback.

Do you think I should close the issue?

0reactions
fingoldocommented, Nov 6, 2020

Thanks for your interest @Kerang !

I already worked around the problem using this bit of code in thread run method:

from subprocess import call
phrase = "Hi everyone"
call(["python3", "speak.py", phrase])

where speak.py is:

import sys
import pyttsx3

def init_engine():
    engine = pyttsx3.init()
    return engine

def say(s):
    engine.say(s)
    engine.runAndWait() #blocks

engine = init_engine()
say(str(sys.argv[1]))

I never posted this because I thought it wasn’t a solution, but just a work around. Frankly I was waiting for the developers of pyttsx to resolve this problem. I think pyttsx may be the best text-to-speech engine around at the moment and not being able to use it in a OSX thread is a real drawback.

Do you think I should close the issue?

I’d rather say Popen instead of call, to make it async.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use pyttsx in a python thread - Stack Overflow
If you just need to convert text into speech, you can use os.system('say %s') : import os def myfunc(): os.system('say ok').
Read more >
Python | Text to Speech by using pyttsx3 - GeeksforGeeks
it is a very easy to use tool which converts the entered text into speech. The pyttsx3 module supports two voices first is...
Read more >
An Intro to Threading in Python
In this intermediate-level tutorial, you'll learn how to use threading in your Python programs. You'll see how to create threads, how to coordinate...
Read more >
python threading - multiple parameter and return
I would use mutable variable, which is sometimes used. Lets create special class which will be used for storing results from thread functions....
Read more >
While using pyttsx 3 I am having the following ERROR i have ...
Made a check for package pypiwin32, which it is actually installed. C:\Users\JKN010\AppData\Roaming\Python\Python38\site-packages\ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found