Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6199459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:11:36+00:00 2026-05-24T04:11:36+00:00

This is the program that works (I am able to hear the text to

  • 0

This is the program that works (I am able to hear the text to speech in action):

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()

If the program is changed to

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()

Then it gets ‘stuck’ at the line “engine.runAndWait()” and text to speech doesn’t work. I am guessing that the problem lies with the rules of scoping with threads. right?
Basically what I want is a.. a ‘handle’ to the engine variable in my main thread. So that i can call engine.stop() from the main thread.

Hope I made sense

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:11:37+00:00Added an answer on May 24, 2026 at 4:11 am

    Global variables are almost always a bad approach. You can just pass engine as an argument to saythread:

    def saythread(engine, location, text):
        engine.say(text)
        engine.runAndWait()        
    
    # ...later...
    
    engine = pyttsx.init()                 #(CHANGED) ADDED VARIABLE 
    t = threading.Thread(target=saythread,args=(engine, 1, "here we go"),name='sayitthread')
    t.start()
    

    But I’d be willing to bet that the real problem is that the methods on your engine object aren’t designed to be called from other threads. (Scoping has nothing to do with it, by the way, it depends on the design of the underlying library.)

    It might be that you can get around the problem using a lock:

    def saythread(engine, lock, location, text):
        with lock:
            engine.say(text)
            engine.runAndWait()  
    
    engine = pyttsx.init()
    lock = threading.Lock()
    t = threading.Thread(
            target=saythread,
            args=(engine, lock, 1, "here we go"),
            name='sayitthread')
    t.start()
    

    …but that will only help if you were doing something else with your engine object concurrently that it didn’t like. From your code, that doesn’t look like it’s the case, so you might have to live with creating each engine object within the thread, and finding another way to pass around state. Alternatively, you can keep your engine in the main thread, and use a queue to call it from other threads where you’re doing other work.

    It’s worth remembering that not all Python libraries (especially those that are written in C) support threads at all, so you have to be careful, read the documentation or ask the author.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program that uses qhttp to get a webpage. This works
There's this program, pdftotext, that can convert a pdf file to a text file.
I'm writing a program that works with files. I need to be able to
I am building a C# program that unzips a file, and work on this
So I have this program that I really like, and it doesn't support Applescript.
I have this program that should execute a piece of code base on the
I bought this program that created a pretty nice imageuploader flash script however I
I have a C/ncurses program that I'm debugging/maintaining. This program does ripoffline twice: first,
I'm having some trouble returning multiple values in this program that calculates min, max,
For some reason this program is saying that 'switch' is not defined. What is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.