1

When running the following program:

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

If I comment out the Speakit in the main, the script run correctly, speaking what prints out

Watson $ python3 mp2.py
0
running Speakit
Argument is zero
5
running Speakit
Argument is not zero
We're done

It I don't comment out the Speakit in the main, the script will just speak the "Start" word and then not speak again and just hangs

 python3 mp2.py
running Speakit
0
running Speakit

Don't understand why

Hankp
  • 47
  • 5

1 Answers1

0

I have given your code a quick spin. At first, the interpreter threw some major errors due to the initialisation of your pyttsx engine. I deleted the 'espeak' argument and then it did work and the program also did speak both lines.

Maybe use try/except in order to troubleshoot.

thomin
  • 81
  • 5
  • I also deleted 'espeak' from the init but got same result. try/except did not show anything.. I did find out that it seems to hang on the tts = _TTS() when is is trying to say "argument is 0". But don't know why. – Hankp Aug 11 '20 at 22:05
  • I could imagine that the first loop is still running, at least I encountered that problem before. Maybe try putting thisat the beginning of your start() function: `try: engine.endLoop() except Exception as e: pass` – thomin Aug 13 '20 at 19:13
  • Tried what you suggested, no luck. I did find out that if I do all the processing in the main script it works fine. So it seems the problem has something to do with running it once in the main and the other times in the sub-processes. Unfortunately this is just a test script. My actual script requires subprocessing.; – Hankp Aug 15 '20 at 01:06