0

For example:

#!/usr/bin/env python3

# cmd.py

import time
for i in range(10):
    print("Count %d" % i)
    time.sleep(1)

#!/usr/bin/env python3

import subprocess

# useCmd.py

p = subprocess.Popen(['./cmd.py'], stdout=subprocess.PIPE)
out, err = p.communicate()
out = out.decode()
print(out)

In useCmd.py I can print out the output of cmd.py, but only after it's finished outputting. How can I print out it in realtime and still have it stored in a string? (sort of like tee in bash.)

qed
  • 21,094
  • 18
  • 110
  • 180

1 Answers1

1

If you don't have to deal with stdin, you could avoid using communicate that is blocking, and read directly from the process stdout until your stdout ends:

p = subprocess.Popen(['python', 'cmd.py'], stdout=subprocess.PIPE)
# out, err = p.communicate()
while True:
    line = p.stdout.readline()
    if line != '':
        print line,
    else:
        break

related

Community
  • 1
  • 1
Dariosky
  • 101
  • 2
  • 6