reading shell output in parallel

Reid Nichol rnichol_rrc at yahoo.com
Sat Aug 14 01:44:48 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

http://docs.python.org/lib/os-newstreams.html


popen2 module
http://docs.python.org/lib/module-popen2.html

import popen2
cmd_stream = popen2.Popen3(cmd, 1)
return_code = cmd_stream.wait()
for output in cmd_stream.childerr.readlines():
     cmd_output += output


You might just want to use popen2/3/4 instead of the class objects.  It 
just depends on your specific needs.  What is being printed may be from 
   stdout and/or stderr.  Experiment to see what's really coming from where.



More information about the Python-list mailing list