Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading