0

I've tried different methods to print the output of Popen while command is being executed,none of them has worked for me so far,what am I missing?how to get real-time output using Popen

import signal, os
import sys
import subprocess
import argparse
from subprocess import Popen, PIPE, STDOUT
import threading
from time import sleep
import time

from subprocess import Popen, PIPE, STDOUT
cmd = "python \\\\snowcone\\builds724\\INTEGRATION\\BUILD-117493-STD.INT-1\\uncommon\\build\\script.exe -s ufs"
#proc = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE)

proc = subprocess.Popen(cmd.split(' '), stderr=subprocess.PIPE)
print "Executing %s"%cmd
# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, proc)
signal.alarm(5)

with proc.stderr:
    for line in iter(proc.stderr.readline, b''):
        print "LINE"
        print line,

proc.wait() # wait for the subprocess to exit
  • Possible duplicate of [How to get output from subprocess.Popen()](http://stackoverflow.com/questions/1388753/how-to-get-output-from-subprocess-popen) – en_Knight Jun 30 '16 at 21:31
  • @en_Knight - I've already tried that but it doesnt work,I have tried 3 methods in vain,please help –  Jun 30 '16 at 21:36
  • I tried `http://stackoverflow.com/questions/1388753/how-to-get-output-from-subprocess-popen` which still doesnt work... –  Jun 30 '16 at 21:47
  • 1
    So you used `for line in iter(proc.stdout.readline, "")`? Also are you sure the output is not going out stderr? – Padraic Cunningham Jun 30 '16 at 21:54
  • @PadraicCunningham - Excellent,you are right,that was the problem, also is there a way to wait for the subprocess to exit only for a definite amount of time when `script.exe` is hung or something and then terminate it –  Jun 30 '16 at 22:28
  • how to we use the signal in this case considering the signal need to be send to Popen –  Jun 30 '16 at 22:36
  • I updated my latest code,please suggest where to handle the call proc.terminate() or proc.kill() in the call back –  Jun 30 '16 at 22:42
  • http://stackoverflow.com/questions/29316426/how-to-timeout-when-i-use-subprocess/29316635#29316635 I would also ask a new question in place of editing this one. Not sure how you will know if it is hung though bar you get not output and if that happens you won't do much as stderr will block – Padraic Cunningham Jun 30 '16 at 22:51
  • why would the output go to `stderr`,also if I added`stdout=subprocess.PIPE` to proc as `proc = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE,stderr=subprocess.PIPE)` ,it doesnt' print then,what could be going wrong? –  Jul 01 '16 at 01:03

0 Answers0