How to get stdout from a child process OF a child process as it runs?

I’m running a python script that triggers a script inside a docker container, which prints out its start time, starts another process with arguments, and then prints out its end time, after the process it triggered is done.

I’ve been using this code to trigger the script in the container and get its output:

pr = subprocess.Popen('docker exec ' + docker_container + ' /scripts/', cwd=os.path.dirname('/'),
                                                shell=True, stdout=subprocess.PIPE, stderr = subprocess.PIPE )
        if debug:
                while True:
                        output = pr.stdout.readline().decode('utf-8')
                        if output == '' and pr.poll() is not None:
                        if output:
                (out, error) = pr.communicate()
                print("### re-index: error message: " + error.decode('utf-8'))
                print("### re-index: message: " + out.decode('utf-8')) 

When I run in debug mode I want to be able to see all of the output lines from the process triggered by the script inside the container, as it runs, so I could see if it hangs, stop it, and adjust its settings.

Problem is, when I try it, I only get the start time line from the script inside the container, on the fly. The rest of the lines (the output from the process the script inside the container triggered) only come through after the process the script inside the container triggered is done running.

Is there any way I could adjust my code to get the output of BOTH my child process (= the script inside the container) and its child process (=the process it triggers) as they run, and not after the child’s child process ends?

Other than cutting out the middleman and triggering the child’s child’s process directly, that is, which I really would like to avoid.

Source: StackOverflow