Search code examples
pythonprocesspython-multiprocessingpython-multithreading

python subprocess send stout on the fly when running


Is there a way to child.py sends stout "on the fly", when running?
Or main.py needs to wait child.py to terminate?

In these scripts, main.py needs to wait 5 seconds to start printing all lines.
I want that process.stdout.readline() get the last print in child.py when child.py still running.

main.py

import subprocess
import time
process = subprocess.Popen(["./child.py"], stdout=subprocess.PIPE)
i = 1
while i < 5:
  print(process.stdout.readline()) #to print, child.py needs to terminate before
  time.sleep(1)
  i+=1

child.py

#!/usr/bin/env python3
# coding=utf-8

import sys
import time

def run():
  i = 1
  while i < 5:
    time.sleep(1)
    print(f'ok {i}')
    i+=1

if __name__ == "__main__":
    run()

Solution

  • In child.py you wrote this:

        print(f'ok {i}')
    

    Replace it with this:

        print(f'ok {i}', flush=True)
    

    When testing interactively isatty() returns True, so child.py will default to unbuffered behavior. Each line of output will appear immediately.

    When running as a subprocess connected to a pipe, you are seeing it default to buffered behavior. Use a flush() call to defeat this.