one function keeps getting called in multi threading

by: sahilshukla, 7 years ago

Last edited: 7 years ago

I am trying to thread a speech_recognition function to run in the background continuously and the checkingAudio function to see what text was spoken and take actions accordingly, I tried to thread the 2 functions to run in parallel but the speech recon function is getting called over and over again, I have never worked with threading and followed a tutorial on youtube to thread my functions, I get that I could have made a very stupid mistake so I request the person who answers the question to be a little elaborate in their answer and my mistake. Thank you.


class listen(threading.Thread):

    def __init__(self):

        self.playmusicobject = playmusic()
        self.r  = sr.Recognizer()

        self.listening()

    def listening(self):

        while 1:
            self.objectspeak = speak()
            self.apiobject = googleAPI()
            print("say something")
            time.sleep(0.5)
            with sr.Microphone() as source:
                # self.objectspeak.speaking("say something")
                self.audio = self.r.listen(source)


    def checkingAudio(self):
        time.sleep(0.5)

        try:
            a = str(self.r.recognize_google(self.audio))
            a = str(self.r.recognize_google(self.audio))
            print(a)

            if a in greetings:
                self.objectspeak.speaking("I am good how are you?")

            if a in music:
                print("playing music")
                self.playmusicobject.play()
            if a in stop:
                print("stopping")
                self.playmusicobject.b()

            if a in api:
                self.apiobject.distance()

            else:
                print("error")

        except sr.UnknownValueError:
            print("Google Speech Recognition could not understand audio")

        except sr.RequestError as e:
            print("Could not request results from Google Speech Recognition service; {0}".format(e))


class speak:
    THIS IS A PYTTS class




class googleAPI:
    GOOGLE DISTANCE API function calculates distance between 2 places

class playmusic:

    def play(self):
        self.objectspeak = speak()
        playsound.playsound('C:UserslegionDownloadsMusicmerimeri.mp3')

    def b(self):
        self.objectspeak.speaking("music stopped")

while 1:
    t1 = threading.Thread(target=listen())
    t2 = threading.Thread(target= listen.checkingAudio())
    t1.join()
    t2.join()










You must be logged in to post. Please login or register an account.



So I just deleted a While loop in my listening statement, and after that I got the error TypeError: checkingAudio() missing 1 required positional argument: 'self' which as far as I can guess is that my function checkingAudio is at least getting called I'll update my post as soon as I get it working

-sahilshukla 7 years ago

You must be logged in to post. Please login or register an account.

As it turns out, I didn't use threading at all, I just called the functions in my main thread. I didn't make them the target nor did I used
 t1.start 
anyways here's the working code,



while 1:
    listener = listen()  # Make the object
    t1 = threading.Thread(target=listener.listening) # Note: No parens or we invoke in main thread
    t2 = threading.Thread(target=listener.checkingAudio) # Note: No parens
    t1.start()  # Actually launch threads
    t2.start()
    t1.join()
    t2.join()



-sahilshukla 7 years ago

You must be logged in to post. Please login or register an account.