0

I'm a new bie to python programming and programming as well. so kindly excuse if this sound to be a basic question.

I'm developing a tool which on course performs make clean and make on git.

I'm using subprocess module for running these make commands.

One observation is: I use communicate to see the results of the make commands. the issue with communicate is, it waits for the subprocess.popen to finish and then sends the output.

if the make command runs for 30 mins, there is no output on the screen for 30 mins and then communicate sends the entire data what it had collected for the 30 mins.

Question is: Is there a way to get the output from the subprocess as and when there is something displayed on stdout and then print it out on the screen?

The code that I had written so far is:

def makeClean(path,cmd1='make',cmd2='clean'):
    print '+++++++++++ RUNNING MAKE CLEAN  +++++++++++ \n'
    c = subprocess.Popen([cmd1 , cmd2],stdout = subprocess.PIPE, stderr = subprocess.PIPE,cwd = path)
    out = c.communicate()[0]
    for line in out.split('\r\n'): print line

Any thoughts on this? I tried using pexpect.run() but that did not work for me...

-Vijay

user596922
  • 1,501
  • 3
  • 18
  • 27

1 Answers1

2

If the output should go to stdout, then don't redirect it back to the calling process. Just

subprocess.call([cmd1, cmd2], cwd=path)

will do. If you do need to process the output in some way, then don't use communicate but read from the pipe directly:

proc = subprocess.Popen([cmd1, cmd2], stdout=subprocess.PIPE, cwd=path)
for ln in proc.stdout:
    process(ln)
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • my script prints the script log to the console and to a file. When I used the subprocess.call, the script log never went to a file.. But the second solution of subprocess.Popen worked for me.. Thanks for the input. – user596922 Jul 05 '12 at 06:11
  • One more quick question.. I used the second suggestion but at the end of every line it inserted a new line .. how do I get over with it? – user596922 Jul 05 '12 at 14:09
  • @user596922: it doesn't *insert* a newline, it *preserves* the newline that was in the original input, as Python's text IO methods always do. You can call the `strip` method on the lines to remove it. – Fred Foo Jul 05 '12 at 14:15