Fetching the stdout & stderr as it flows from a unix command.

Discussion in 'Python' started by Hans Deragon, Jun 4, 2004.

  1. Hans Deragon

    Hans Deragon Guest

    Greetings.


    I am performing:

    commands.getstatusoutput("rsync <params>");

    rsync takes a long time and prints out a steady stream of lines
    showing which file it is currently working on.

    Unfortunatly, the commands.getstatusoutput() fetchs everything and
    there is no way to ask it to copy the output of the command to
    stdout/stderr. Thus when rsync takes an hour, I have no clue where it
    is at.

    Anybody has a suggestion how to let stdout/stderr print out on the
    console when calling a unix command?


    Best regards,
    Hans Deragon
    --
    Consultant en informatique/Software Consultant
    Deragon Informatique inc. Open source:
    http://www.deragon.biz http://facil.qc.ca (Promotion du libre)
    mailto:// http://autopoweroff.sourceforge.net
    (Logiciel)
    Hans Deragon, Jun 4, 2004
    #1
    1. Advertising

  2. Hans Deragon

    Matt Leslie Guest

    Hans Deragon wrote:
    > Greetings.
    >
    >
    > I am performing:
    >
    > commands.getstatusoutput("rsync <params>");
    >
    > rsync takes a long time and prints out a steady stream of lines
    > showing which file it is currently working on.
    >
    > Unfortunatly, the commands.getstatusoutput() fetchs everything and
    > there is no way to ask it to copy the output of the command to
    > stdout/stderr. Thus when rsync takes an hour, I have no clue where it
    > is at.
    >
    > Anybody has a suggestion how to let stdout/stderr print out on the
    > console when calling a unix command?
    >


    Perhaps you could use the popen2 module. popen2.popen2() returns file
    handles for stdout and stderr. You could read from these and print the
    output to the screen while the process runs.

    Matt
    Matt Leslie, Jun 4, 2004
    #2
    1. Advertising

  3. Hans Deragon

    Donn Cave Guest

    In article <c9qi42$908$>,
    Matt Leslie <> wrote:

    > Hans Deragon wrote:
    > > Greetings.
    > >
    > >
    > > I am performing:
    > >
    > > commands.getstatusoutput("rsync <params>");
    > >
    > > rsync takes a long time and prints out a steady stream of lines
    > > showing which file it is currently working on.
    > >
    > > Unfortunatly, the commands.getstatusoutput() fetchs everything and
    > > there is no way to ask it to copy the output of the command to
    > > stdout/stderr. Thus when rsync takes an hour, I have no clue where it
    > > is at.
    > >
    > > Anybody has a suggestion how to let stdout/stderr print out on the
    > > console when calling a unix command?
    > >

    >
    > Perhaps you could use the popen2 module. popen2.popen2() returns file
    > handles for stdout and stderr. You could read from these and print the
    > output to the screen while the process runs.


    Carefully, though. If there's no particular reason to keep
    the two streams distinct, then it might be a good idea to
    merge them, so there is only one file to read.

    Otherwise, to read two files in parallel, see select.select.

    With select, I would not use the file objects that Popen3 creates,
    rather use the pipe file descriptors directly (the integer number
    returned by the file object fileno() method, which can be used
    with core UNIX/POSIX I/O functions like os.read.) File objects
    are buffered, which makes select more or less useless; you can
    turn off buffering, but that makes functions like readline()
    horribly inefficient. It isn't very hard to live without the
    file object here.

    And then note that rsync may not deliver its output as promptly
    when talking to a pipe. That can be solved too, but it's
    usually not worth the trouble.

    Donn Cave,
    Donn Cave, Jun 4, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Landenburer

    redirecting stderr and stdout

    Jon Landenburer, May 12, 2004, in forum: Perl
    Replies:
    1
    Views:
    11,206
    Joe Smith
    May 13, 2004
  2. katthi

    work flows in ruby on rails

    katthi, Jun 2, 2007, in forum: Ruby
    Replies:
    1
    Views:
    82
    Tim Hunter
    Jun 2, 2007
  3. Replies:
    6
    Views:
    173
    Arlen Cuss
    Mar 14, 2008
  4. it_says_BALLS_on_your forehead
    Replies:
    2
    Views:
    263
    Joe Smith
    Jan 10, 2006
  5. Joe
    Replies:
    2
    Views:
    220
Loading...

Share This Page