A test program
#!/usr/bin/env python3
import sys
count = 0
sys.stderr.write('stderr, order %d\n' % count)
count += 1
sys.stdout.write('stdout, order %d\n' % count)
count += 1
sys.stderr.write('stderr, order %d\n' % count)
count += 1
sys.stdout.write('stdout, order %d\n' % count)
when invoked through the terminal, the expected output is,
stderr, order 0
stdout, order 1
stderr, order 2
stdout, order 3
In the interactive shell, when I redirect stdout
to a PIPE, the output order is different from the output above, where Popen would group stderr
and write them all and then do the same thing to stdout
, instead of interleaving stdout and stderr.
In [29]: a = sp.run(['./test.py'], stderr=sp.STDOUT)
stderr, order 0
stdout, order 1
stderr, order 2
stdout, order 3
In [30]: a
Out[30]: CompletedProcess(args=['./test.py'], returncode=0)
In [33]: b = sp.Popen(['./test.py'], stderr=sp.STDOUT, stdout=sp.PIPE, encoding='utf-8')
In [34]: print(b.communicate()[0])
stderr, order 0
stderr, order 2
stdout, order 1
stdout, order 3