Connecting Tech Pros Worldwide Forums | Help | Site Map

reading shell output in parallel

Steve
Guest
 
Posts: n/a
#1: Jul 18 '05
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

Reid Nichol
Guest
 
Posts: n/a
#2: Jul 18 '05

re: reading shell output in parallel


Steve wrote:[color=blue]
> 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[/color]

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.
Reid Nichol
Guest
 
Posts: n/a
#3: Jul 18 '05

re: reading shell output in parallel


Steve wrote:[color=blue]
> 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[/color]

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.
P@draigBrady.com
Guest
 
Posts: n/a
#4: Jul 18 '05

re: reading shell output in parallel


Steve wrote:[color=blue]
> 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.[/color]

Better support is being added for this.
In the meantime you could use this:
http://www.pixelbeat.org/libs/subProcess.py
With this you specify the timeout in seconds
so you would update client every 1 second rather
than every read (around 4K) which may or
may not be what you want.

Usage is like:

import subProcess
process = subProcess.subProcess("your shell command")
while process.read(1):
handle(process.outdata, process.errdata)
del(process)

Pádraig.
Closed Thread


Similar Python bytes