By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Grabbing the output of a long-winded shell call (in GNU/Linux)

P: n/a
Hello,

Suppose I want to run from within a Python GUI app some long-output
shell call. For example, from within Python I might want to call

g++ foo.cpp

I already know there are many ways to do this, e.g.,
commands.getstatusoutput('g++ foo.cpp') to name one.

The problem is that this might generate a ton of output (e.g.,
because of compilation errors), and might take a while to do so. In my
GUI, I'd like to print out the output as it's being generated, not wait
until all is done (as commands.getstatusoutput will do) and dump it at
once.

So my question is if there's a way to "grab" the output as it's being
generated. It doesn't matter if the solution is blocking (as opposed to
callback based), since threads can handle this. I just don't know how to
"grab" the output. I appreciate your time in reading (and answering
this), as I've been googling several hours for this.
Many Thanks,

E
May 1 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On May 1, 2:23 pm, Efrat Regev <efrat_re...@yahoo.comwrote:
So my question is if there's a way to "grab" the output as it's being
generated. It doesn't matter if the solution is blocking (as opposed to
callback based), since threads can handle this. I just don't know how to
"grab" the output. I appreciate your time in reading (and answering
this), as I've been googling several hours for this.
There may be more pythonic solution than what I suggest here but this
is what I have done when I needed similar functionality. Basically run
your command in the background and redirect its stdout/err to a temp
file. You may run the command either in the background or in a
separate thread. You can then run the command "tail --retry --
pid=<pid-n+0 -F <output_file>" and grab the output. The tail command
exits once the real command is done.

Raghu.


May 1 '07 #2

P: n/a
dr*******@gmail.com wrote:
On May 1, 2:23 pm, Efrat Regev <efrat_re...@yahoo.comwrote:
>So my question is if there's a way to "grab" the output as it's being
generated. It doesn't matter if the solution is blocking (as opposed to
callback based), since threads can handle this. I just don't know how to
"grab" the output. I appreciate your time in reading (and answering
this), as I've been googling several hours for this.

There may be more pythonic solution than what I suggest here but this
is what I have done when I needed similar functionality. Basically run
your command in the background and redirect its stdout/err to a temp
file. You may run the command either in the background or in a
separate thread. You can then run the command "tail --retry --
pid=<pid-n+0 -F <output_file>" and grab the output. The tail command
exits once the real command is done.

Raghu.

Many Thanks! I'll try this
May 1 '07 #3

P: n/a
Efrat Regev schrieb:
dr*******@gmail.com wrote:
>On May 1, 2:23 pm, Efrat Regev <efrat_re...@yahoo.comwrote:
>>So my question is if there's a way to "grab" the output as it's being
generated. It doesn't matter if the solution is blocking (as opposed to
callback based), since threads can handle this. I just don't know how to
"grab" the output. I appreciate your time in reading (and answering
this), as I've been googling several hours for this.

There may be more pythonic solution than what I suggest here but this
is what I have done when I needed similar functionality. Basically run
your command in the background and redirect its stdout/err to a temp
file. You may run the command either in the background or in a
separate thread. You can then run the command "tail --retry --
pid=<pid-n+0 -F <output_file>" and grab the output. The tail command
exits once the real command is done.
Or instead use the python subprocess module and read the commands
stdin/out/err from the Popen-object.

Diez
May 1 '07 #4

P: n/a
Diez B. Roggisch wrote:
Efrat Regev schrieb:
>dr*******@gmail.com wrote:
>>On May 1, 2:23 pm, Efrat Regev <efrat_re...@yahoo.comwrote:

So my question is if there's a way to "grab" the output as it's being
generated. It doesn't matter if the solution is blocking (as opposed to
callback based), since threads can handle this. I just don't know
how to
"grab" the output. I appreciate your time in reading (and answering
this), as I've been googling several hours for this.

There may be more pythonic solution than what I suggest here but this
is what I have done when I needed similar functionality. Basically run
your command in the background and redirect its stdout/err to a temp
file. You may run the command either in the background or in a
separate thread. You can then run the command "tail --retry --
pid=<pid-n+0 -F <output_file>" and grab the output. The tail command
exits once the real command is done.

Or instead use the python subprocess module and read the commands
stdin/out/err from the Popen-object.

Diez
Excellent, thanks!

BTW:
http://aspn.activestate.com/ASPN/Coo.../Recipe/440554

(found this after seeing responses on list)
May 1 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.