473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Joining stdout & stderr of subprocess ?


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
Apr 21 '06 #1
4 1837
robert wrote:

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.
Apr 21 '06 #2
>>>>> robert <no*****@no-spam-no-spam.com> (R) wrote:
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.

--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Apr 21 '06 #3
robert <no*****@no-spam-no-spam.com> wrote:
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

--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Apr 21 '06 #4
On Fri, 21 Apr 2006 09:30:06 -0500, Nick Craig-Wood wrote:
robert <no*****@no-spam-no-spam.com> wrote:
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

[...]

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 ;).
--
Stephane
Apr 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Roman Neuhauser | last post by:
Hello, I have a piece of code that gets run in a script that has its stdout closed: import sys sys.stdout = sys.stderr c = subprocess.Popen (..., stdin = subprocess.PIPE,
1
by: Jacek Popławski | last post by:
Popen from subprocess module gives me access to stdout, so I can read it. Problem is, that I don't know how much data is available... How can I read it without blocking my program? example:...
3
by: mikem76 | last post by:
How do I automatically redirect stdout and stderr when using os.popen2 to start a long running process. If the process prints a lot of stuff to stdout it will eventually stop because it runs out...
0
by: Christoph Haas | last post by:
Evening, I'm having trouble with running a process through Python 2.4's subprocess module. Example code: ======================================================== def run(command): run =...
3
by: alf | last post by:
Hi, I use subprocess to execute another program but need to suppress its stdout. I can achieve it by using Popen(...,stdout=subprocess.PIPE,...) but wonder where the all stdout actually goes. Is...
3
by: Fuzzyman | last post by:
Hello all, Before I ask the question a couple of notes : * This question is for implementing a script inside the Wing IDE. For some reason using the subprocess module doesn't work so I need a...
3
by: Pappy | last post by:
SHORT VERSION: Python File B changes sys.stdout to a file so all 'prints' are written to the file. Python file A launches python file B with os.popen("./B 2>&^1 >dev/null &"). Python B's output...
2
by: Greg Ercolano | last post by:
When I use os.popen(cmd,'w'), I find that under windows, the stdout of the child process disappears, instead of appearing in the DOS window the script is invoked from. eg: C:\type foo.py import...
2
by: newlasdjfk | last post by:
I have read tons of posts but still can't seem to figure it out. I want to subprocess.Popen() rsync.exe in windows, and print the stdout in python. This works... import subprocess, time, os,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.