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

Discussion in 'Python' started by Efrat Regev, May 1, 2007.

  1. Efrat Regev

    Efrat Regev Guest

    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
     
    Efrat Regev, May 1, 2007
    #1
    1. Advertisements

  2. Efrat Regev

    draghuram Guest

    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.
     
    draghuram, May 1, 2007
    #2
    1. Advertisements

  3. Efrat Regev

    Efrat Regev Guest

    Many Thanks! I'll try this
     
    Efrat Regev, May 1, 2007
    #3
  4. Or instead use the python subprocess module and read the commands
    stdin/out/err from the Popen-object.

    Diez
     
    Diez B. Roggisch, May 1, 2007
    #4
  5. Efrat Regev

    Efrat Regev Guest

    Efrat Regev, May 1, 2007
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.