0

I have a compiled program called program that takes in 1 argument called 2phase_eff. I would like to run this program from python but also be able to view its progress (it outputs various progress messages) on the shell in real time. So far I have succeeded in actually running it and viewing output after it is done running using the following code:

import subprocess
subprocess.Popen("program 2phase_eff", stdout=subprocess.PIPE, shell=True).communicate() 

Yes this does output all the intermediate stuff at the very end but there are two problems

  1. I cannot see the cmd shell and
  2. The output is not in real time

How can I tweak the above command to fulfill above two objectives? Thanks.

user32882
  • 5,094
  • 5
  • 43
  • 82

1 Answers1

0

To show the command shell you need to pass a value for creationflags to your call to subprocess.Popen(). In Windows this is the nShowCmd parameter of ShellExecute(). It's an integer between 0 and 10. Popen()'s default is zero, which corresponds to SW_HIDE. You can find a full list of the available values at https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx .

The lack of real-time output is a consequence of using Popen.communicate() in combination with stdout=subprocess.PIPE. The output is buffered in memory until the subprocess completes. That is because .communicate() returns you the output, and you don't get that until the method call returns.

You could try passing it a file descriptor instead, and poll that.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • so what is the end command I should use to achieve the desired result? – user32882 Jun 04 '17 at 14:55
  • @user32882: I can't answer that because I don't know what the desired result is. `Popen.communicate()` waits until the subprocess has finished. So if you want to see output before that don't pipe the output. What you do depends on what you want. You could change your command to `"program 2phase_eff" > myfile.txt` and not set `stdout`. You asked for a tweak. There really isn't one. – BoarGules Jun 06 '17 at 07:37