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

E

Efrat Regev

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
 
D

draghuram

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.
 
E

Efrat Regev

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
 
D

Diez B. Roggisch

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

Diez
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top