reading shell output in parallel

Reid Nichol rnichol_rrc at yahoo.com
Sat Aug 14 01:58:09 EDT 2004


Steve wrote:
> Hi,
> 
> I'm pretty new to python.  I am trying to write a simple application
> that can read the stdout output from a command in linux.  I've tried
> using x = commands.getstatusoutput() but this only gives back the
> output in x after finished executing.  I would like to read the
> contents as it is being shown on the screen and then send parts of
> this info over a simple client/server setup.  I have the client/server
> part set up already.
> 
> Can anyone suggest a simple way to do this?  
> 
> Thanks for your help, I appreciate it.
> 
> Steve

One last thought before my zzzz.  For you, you would probably want 
something like:

import popen2

cmd_stream = popen2.Popen3(cmd, 1)
while (cmd_stream.poll() == 1):
     for line in cmd_stream.childerr.readlines():
         #process

for line in cmd_stream.childerr.read_lines():
     #clean up remaining lines not process already


To do it in parallel.  I'd be interested to know if the above will 
actually work ;)  At any rate it'll get you started.



More information about the Python-list mailing list