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

H

Hans Deragon

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://[email protected] http://autopoweroff.sourceforge.net
(Logiciel)
 
M

Matt Leslie

Hans said:
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
 
D

Donn Cave

Matt Leslie said:
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, (e-mail address removed)
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top