i've got problem popen , pipes on windows. think popen mixed data between stdout , stderr. when program reads stdout ok when read stdout , stderr data stdout thrown stderr. read method:
line in iter(self._read_obj.readline, b''): self._queue.put(line) sleep(.2)
where self._read_obj either stderr or stdout. know how can solve this?
from subproccess import * x = popen('sh /root/script.sh', stdout=pipe, stderr=pipe, stdin=pipe, shell=true) print x.stdout.readline() print x.stderr.readline()
since don't posted relevant code, here's how should look. note readline still give output tho there none fetch, stderr "hangs" waiting output, way go if you're using subprocess.
from subprocess import pipe, popen x = popen(['ls', '-l'], stdout=pipe, stderr=pipe, stdin=pipe, shell=false) while 1: if x.poll() != none: break _queue.put(x.stdout.readline()) sleep(0.2)
something should work. if don't need separate 2 outputs, , want read data , att queue, can do:
x = popen(['ls', '-l'], stdout=pipe, stderr=stdout, stdin=pipe, shell=false)
here's do
(and i'm not saying best practice of things, works)
from threading import thread, enumerate subprocess import popen, pipe time import sleep class nonblockingstderr(thread): def __init__(self, handle): self.handle = handle self.stderroutput = [] thread.__init__(self) self.start() def stderr(self): if len(self.stderroutput) <= 0: return '' else: ret = self.stderroutput[0] self.stderroutput = self.stderroutput[1:] return ret def run(self): while 1: line = self.handle.readline() if len(line) > 0: self.stderroutput.append(line) sleep(0.1) subprocess import pipe, popen x = popen(['ls', '-l'], stdout=pipe, stderr=pipe, stdin=pipe, shell=false) errhandle = nonblockingstderr(x.stderr) while 1: if x.poll() != none: break _queue.put(errhandle.stderr()) _queue.put(x.stdout.readline()) sleep(0.2)
Comments
Post a Comment