Getting stdout and stderr from subprocess in correct order
ben+python at benfinney.id.au
Sat Mar 4 00:17:30 EST 2017
"Ivan \"Rambius\" Ivanov" <rambiusparkisanius at gmail.com> writes:
> When I run it through subprc.py, I do get standard out and standard
> errors streams, but they are separated and not in the order above
That's right. You have two entirely distinct streams, they don't attempt
to co-ordinate and none of the information in the stream inherently
relates to any other stream.
Note that your experience:
> When i run hw.py I get
> $ ./hw.py
is not something your program has access to. For all that it knows, you
could be redirecting each stream to separate files::
$ ./hw.py 1> foo.out 2> bar.out
and then the absence of any relation between the content in each stream
becomes more obvious.
> How should I use subprocess in order to get the outputs in the correct
The “correct” order is something additional that you're wanting to
impose. So you'll need to define that yourself.
If you want some correspondence between the content in different
streams, you will need to decide for yourself what that correspondence
One obvious way to do it is to put a timestamp as part of the content
written to each stream::
Then, the ordering of content in the output can be inferred later by
comparing the timestamps.
If that's of interest, you should probably stop using plain ‘print’ and
instead look into the ‘logging’ module, which has good support for
multiple streams, timestamped messages, and many other related things
\ “We live in capitalism. Its power seems inescapable. So did the |
`\ divine right of kings.” —Ursula K. LeGuin, National Book Awards |
_o__) acceptance speech, 2014-11-19 |
More information about the Python-list