Processing messages from a child process thorough stderr and stdout with Python -


my python code spawns child process, , prints out messages both stdout , stderr. need print them differently.

i have following code spawn child process , stdout result it.

cmd = ["vsmake.exe", "-f"] p = subprocess.popen(cmd, stdout=subprocess.pipe) line in iter(p.stdout.readline, ''):     print line,     sys.stdout.flush()     pass p.wait() 

how can modify code check if child process prints out message through stderr also?

added

i need print out stderr , stdout child process prints out something. , cross platform implementation, should run on mac/linux/pc.

p = popen(cmd, bufsize=1024, stdin=pipe, stdout=pipe, stderr=pipe, close_fds=true) p.stdin.close() print p.stdout.read() #this print standard output spawned process print p.stderr.read() #this need, error output <----- 

so error output gets redirected stderr pipe.

if need more in real in time. mean lines printed spawned process prints stdout orstderr` can like:

def print_pipe(type_pipe,pipe):     line in iter(pipe.readline, ''):          print "[%s] %s"%(type_pipe,line),  p = popen(cmd, bufsize=1024, stdin=pipe, stdout=pipe, stderr=pipe, close_fds=true)  t1 = thread(target=print_pipe, args=("stdout",p.stdout,)) t1.start() t2 = thread(target=print_pipe, args=("stderr",p.stderr,)) t2.start()  #optionally can join threads wait till p done. avoidable  # depends on application. t1.join() t2.join() 

in case 2 threads print every time line written either stdout or stderr. parameter type_pipe makes distinction when lines printed know if coming stderr or stdout.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -