1

I've seen this, this, this, this and this. I also know that if PIPE is used with poll() or wait(), there's the danger of a deadlock. So I used communicate().

The problem:
For this code:

def functionToExecuteTheCommand(self, *args):
    processReturnCode = None
    with open("log.txt", 'w') as logfile:
        commandToExecute = args[0]
        self.proc_id = subprocess.Popen(commandToExecute, stdout=logfile, stderr=subprocess.PIPE, shell=True if self.os_platform.lower() == Constants.WINDOWS_PLATFORM_STRING else False)
        (stdout, stderr) = self.proc_id.communicate()
        processReturnCode = self.proc_id.returncode

        # for line in self.proc_id.stdout: 
        #     #sys.stdout.write(line)
        #     print(line)
        #     logfile.write(line)  

    if processReturnCode == 0:
        self.bottomStatusBar().showMessage('Process ran successfully')
    else:
        self.bottomStatusBar().showMessage('SOME ERROR HAPPENED')

When I use Popen(commandToExecute, stdout=logfile, ..., the 'SOME ERROR HAPPENED' message is shown when the program I invoke crashes.

However, if I use Popen(commandToExecute, stdout=subprocess.PIPE, ... and enable the for loop for line in self.proc_id.stdout which is commented out, even when the program I invoke crashes, the message 'SOME ERROR HAPPENED' is not shown.

In both the above cases, the output of the program isn't shown on the commandline until the very end, so while the program runs, it looks like it is hanging, because no output is shown on the commandline. When I used poll() instead of communicate() and used stdout=logfile, the output used to be shown on the commandline normally. But using poll() prevented the ability to detect a crash and write to stdout and to write to the log file simultaneously (due to the deadlock possibility I mentioned earler).

So how can this be achieved?

  • I want the process I invoke, to output the result of print statements to stdout so that it'd be displayed on the commandline.
  • I also want those lines to be written to the log file and
  • Also want to be able to know when the program crashes and when it has completed successfully.

Does subprocess not allow all this simultaneously?

Nav
  • 19,885
  • 27
  • 92
  • 135

0 Answers0