2

I wanted to run a function in parallel to the main program in Python. Suppose I have a speech recognition function. I want it to run in the background and interrupt the main program if a particular utterance is heard. But at the same time, I have other tasks to perform. So, the speech recognition should work as a separate process and may be call a function when a command is heard.

I tried the python multiprocessing module, the thread module and the threading module. But all of these required me to wait until the process or thread is finished. What I want is something that will allow me to run functions in the background. They must call some callback function if a specific event occurs.

I hope I will find an effective way of doing this.

I tried the threading module. The code looked like this (pseudocode):

def detected(text):
    commands = 'a list of commands'
    if text in commands:
        #call some function according to the command

def speech_recognition():
    #while True:
        #If speech detected:
            #record it
            #process it and covert it to text
            #if text is a specified command:
                #call the detected(text) function with the recognized text as argument

import threading as t

pr = t.Thread(target=speech_recognition)
pr.start()

#from here starts the main program that does some other functions that
#doesn't need to be mentioned here.

But this doesn't work. The speech recognition runs for a few seconds and then just quits. No Exceptions raised, no system exits, nothing. Its the same when I try the multiprocessing and thread modules.

2
  • What do you mean the threading module requires you to wait until the thread is finished? You only have to wait if you call join() on a thread, but if you don't you can still do other stuff in the main thread. Commented Jul 27, 2013 at 13:50
  • I edited the post. Just take a look. Commented Jul 28, 2013 at 9:57

2 Answers 2

2

I don't know how CPU-intense speech recognition is, but I am pretty sure the problem you are describing is best solved with maximum decoupling between entities, i.e. separation in processes. Simple scenario: one of your processes runs your "main program", the other process is entirely responsible for speech recognition. You then implement a communication protocol between those processes. The "main program" still needs some kind of event system and asynchronous execution based on threading, because it needs to be able to listen and immediately react to events sent by the speech-reco-process. Hence, a working model would contain:

  • one main process which should not be CPU-bound
  • one child process, spawned via multiprocessing, handling speech recognition
  • a communication protocol enabling the transmission of data/events between main and child process
  • one additional thread in the main process that waits for events sent by the child process and makes sure that the main program reacts correspondingly

Main and child process run concurrently as scheduled by the operating system. Of course this works best on a system with at least two CPU cores. In the main process, the main thread and the other thread to not really run synchronously -- only one thread can run at a time due to CPython's global interpreter lock (GIL).

The communication between main process and child process can be implemented with basic multiprocessing tools such as Queue or Pipe.

As you realize, you need to invest some serious thought into a problem like this. Don't try to solve this quick&dirty or just by trial and error. You need to make sure you understand your self-developed architecture.

Sign up to request clarification or add additional context in comments.

Comments

0

Just use threading and pass your thread either a function handle to call when it has data ready or the application handle in the case of GUI applications so that the thread can create an event with the data attached.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.