473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

reading shell output in parallel

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
Jul 18 '05 #1
3 2060
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.
Jul 18 '05 #2
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.
Jul 18 '05 #3
P
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.


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.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: melih.onvural | last post by:
Group, I want to get into a remote server, tail a file, and see if the last line is an error or not. I think that I've figured out how to shell over and tail the file. I have the specific server...
8
by: zhiwei wang | last post by:
I remember that there is a function that could invoke shell command such as "rm" "cp", directly in .c file. But I could not recall its name, and I googled with nothing meaningful. I vaguely...
2
by: sai bo shan via DotNetMonster.com | last post by:
hi, all i want to know about strobe signal output from parallel port. i want to use parallel port without inpout32.dll. how can i do it? thanks. -- Message posted via...
6
by: Peter | last post by:
I'm interested to know what ideas are out there for reading a parallel port at a constant sample rate while still allowing the user to interact with the GUI. That is, reading it every 10ms for...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
15
by: leorulez | last post by:
Is there any way to read multiple files (more than 1000 files) and then write into one single output file using C? Right now in my program, I have a loop which asks for the filename and writes into...
5
by: bearophileHUGS | last post by:
For array.array "B" means unsigned char, and such arrays accept to be initialized from (str) strings too, this is quite useful: But it seems such capability isn't shared with the append: ...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.