Monitoring output of parallel subprocess in python 2.7 - pocketsphinx
Question: I'm new to python and maybe my problem might be rather obvious and simple.

My goal would be to run a pocketsphinx_continuous in parallel with my own
program. And when needed I would like to poll for output created by
pocketsphinx.

I found different questions and answers related to parallel processing etc in
python. But I need to keep sub-process running until my own program is
running.

I'm using following command:

    
    
    command ='pocketsphinx_continuous -hmm ~/speech/et/models/hmm/est16k.cd_ptm_1000-mapadapt -jsgf ~/speech/et/models/lm/robot2.jsgf -dict ~/speech/et/robot.dict 2>/dev/null | grep "^000"'
    

Output of pocketsphinx will be something like:

0000001:some text;  
0000002: some more text.

Any references or hints are most welcome!

Thank you!

Edit:

Thanks for your response but i solve it as following:

    
    
    def start(self):
           print "start"
           command ='pocketsphinx_continuous -hmm ~/speech/et/models/hmm/est16k.cd_ptm_1000-mapadapt -jsgf ~/speech/et/models/lm/robot2.jsgf -dict ~/speech/et/robot.dict'
           pocketsphinx = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)  
           thread.start_new_thread(self.listen_to_pocketsphinx, (pocketsphinx,))
    
       def listen_to_pocketsphinx(self,pocketsphinx):
           while True:
               line = pocketsphinx.stdout.readline()
               if line.startswith("000"):
                 sentence = line.partition(": ")[2].strip()
                 if sentence:
                     self.direction = sentence
                 else:
                     self.direction = ""
    

I'm not sure that it's the most elegant way but it worked for me.

So for me the case is closed.

Thank you!


Answer: My personal recommendation is that you use an **Asynchronous Application
Framework**. There are many out there with varying features and levels of
simplicity and complexity.

I have a personal bias towards [circuits](http://circuitsframework.com/) and
very generically you would implement something like this (_using circuits-dev
from<https://bitbucket.org/circuits/circuits-dev>_):

    
    
    #!/usr/bin/env python
    
    
    import sys
    
    
    from circuits import Component
    from circuits.io import stdout, Process, write
    
    
    class Ping(Component):
    
        stdout = stdout
    
        def init(self, host):
            self.p = Process(["/bin/ping", host]).register(self)
            self.p.start()
    
        def read(self, data):
            self.fire(write(data), stdout)
    
    
    app = Ping(sys.argv[1])
    app.run()
    

Something like this could also be running a simple web server for example:

    
    
    from circuits.web import Controller, Server
    
    class Root(Controller):
    
        def index(self):
            return "Hello World!"
    
    Server(("0.0.0.0", 8000)).register(app)
    

