Python read from subprocess stdout and stderr separately while preserving order -
i have python subprocess i'm trying read output , error streams from. have working, i'm able read stderr after i've finished reading stdout. here's looks like:
process = subprocess.popen(command, stdout=subprocess.pipe, stderr=subprocess.pipe) stdout_iterator = iter(process.stdout.readline, b"") stderr_iterator = iter(process.stderr.readline, b"")  line in stdout_iterator:     # stuff line     print line  line in stderr_iterator:     # stuff line     print line as can see, stderr loop can't start until stdout loop completes. how can modify able read both in correct order lines come in?
to clarify: still need able tell whether line came stdout or stderr because treated differently in code.
the code in question may deadlock if child process produces enough output on stderr (~100kb on linux machine).
there communicate() method allows read both stdout , stderr separately:
from subprocess import popen, pipe  process = popen(command, stdout=pipe, stderr=pipe) output, err = process.communicate() if need read streams while child process still running portable solution use threads (not tested):
from subprocess import popen, pipe threading import thread queue import queue # python 2  def reader(pipe, queue):     try:         pipe:             line in iter(pipe.readline, b''):                 queue.put((pipe, line))     finally:         queue.put(none)  process = popen(command, stdout=pipe, stderr=pipe, bufsize=1) q = queue() thread(target=reader, args=[process.stdout, q]).start() thread(target=reader, args=[process.stderr, q]).start() _ in range(2):     source, line in iter(q.get, none):         print "%s: %s" % (source, line), see:
Comments
Post a Comment