Joining stdout & stderr of subprocess ?

R

robert

when I run a command

myapp 2>&1


yet:

#!python
print os.popen("myapp 2>&1").read()

the stderr stuff comes all after the stdout stuff.
How can I get ahold of all the out and err joined synchronously in the
order, it is created ?


robert
 
D

Duncan Booth

robert said:
when I run a command

myapp 2>&1
[ I guess you meant to say here that the stdout, stderr output was
interleaved ]
yet:

#!python
print os.popen("myapp 2>&1").read()

the stderr stuff comes all after the stdout stuff.
How can I get ahold of all the out and err joined synchronously in the
order, it is created ?
I think what is happening here is that in the first case the output is to a
terminal, so myapp doesn't buffer its output. In the second case the output
isn't going to a terminal so myapp is buffering it and only produces output
when the buffer is full. Probably there isn't sufficient stderr output to
force any out until myapp terminates, so you get it all at the end.

Probably the only way to get the output interleaved is to change myapp so
that it doesn't buffer its output or so it sends all of its output to a
single stream.
 
P

Piet van Oostrum

robert said:
R> when I run a command
R> myapp 2>&1

R> yet:
R> #!python
R> print os.popen("myapp 2>&1").read()
R> the stderr stuff comes all after the stdout stuff.
R> How can I get ahold of all the out and err joined synchronously in the
R> order, it is created ?


Make sure that myapp writes it synchronously. Probably myapp has it
buffered, so it writes out at the end of the run. It is not caused by
python.
 
N

Nick Craig-Wood

robert said:
when I run a command

myapp 2>&1

Try

myapp 2>&1 | cat

and see what you get. You should get the same output as the python.
#!python
print os.popen("myapp 2>&1").read()

the stderr stuff comes all after the stdout stuff.
How can I get ahold of all the out and err joined synchronously in the
order, it is created ?

You need to either

1) change myapp to fflush() more often

2) investigate the python pty module to fool the app into thinking it
is talking to a terminal
 
S

Stephane Chazelas

Try

myapp 2>&1 | cat

and see what you get. You should get the same output as the python.


You need to either

1) change myapp to fflush() more often

2) investigate the python pty module to fool the app into thinking it
is talking to a terminal
[...]

3) write a LD_PRELOAD wrapper that turns isatty() into a
function that returns true if its arg is 1.

4) perl -pi -e 's/isatty/fchdir/g' myapp
(might not work ;).
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top