'Why does subprocess stdout only produce one line when the process is killed with os.kill()?

I have a piece of code that opens a subprocess and writes the output to a file:

logPath = '/path/to/some/directory/'    
log = open(logPath + 'log', 'a')
guipid = subprocess.Popen(SomeGuiApplication, stdout = log, stderr = subprocess.STDOUT).pid

This brings up the intended application and it's gui. When I close the gui, the output of the gui is correctly shown in the log.

I have another piece of code that lists all instances of the gui application, and closes them with the os.kill() function:

for line in os.popen("ps xa"):
        fields = line.split()
        pid = fields[0]
        process = fields[4]

        if process.find(SomeGuiApplication) > 0:
            os.kill(int(pid), signal.SIGTERM)
                

When I use the os.kill() approach to close the gui application, only the first line of the gui application's output is recorded into the log. I've tried (every?) signal type defined in https://docs.python.org/3/library/signal.html as an input to os.kill(), but I get the same result. Why does the stdout stream from Popen behave differently from an explicitly called signal versus when I close the gui manually?

I am using RHEL7 with python3.6. The GUI application is built in Qt5; it uses the standard Ui::MainWindow construct. The destructor simply calls 'delete ui;'

Note: I'm aware that I could do some rewriting to have the Popen call use stdout = PIPE and then write the lines to a file in 'real' time, but this would require the parent process to keep running for the duration of the child process. This is something I'm looking to avoid.



Solution 1:[1]

I assume that the output of the process is buffered and since you kill it you don't give it a chance to flush its output to stdout.

You could maybe try to change to bufsize parameter of Popen to 0, which should make the output unbuffered:

guipid = subprocess.Popen(SomeGuiApplication, stdout = log, stderr = subprocess.STDOUT, bufsize=0).pid

I would suggest to read the documentation to better understand what you should do.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amit Sides