reading shell output in parallel

S

Steve

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
 
R

Reid Nichol

Steve said:
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.
 
R

Reid Nichol

Steve said:
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.
 
P

P

Steve said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top