1

I've got two files:

run.py

import subprocess
import time

while True:
  time.sleep(1)
  print 'hello'
  proc = subprocess.call(['./writer.sh'])

writer.sh (chmod 777'd)

#!/bin/sh
echo 'write something here'

and I'm confused by the following outputs:

$ python run.py
hello
write something here
hello
write something here
hello
write something here
....

$ python run.py | tee out.log
write something here
write something here
(hello disappears)

....

$ python run.py > out.log
# Nothing, but out.log has the following:
write something here
write something here
write something here
write something here
hello
hello
hello
hello
... # and the two basically "expand" the longer I run this (instead of appending)

What is happening, and how can I get everything to output like the first command?

1 Answer 1

2

The output of your main script is buffered. Call sys.stdout.flush() right before running the subprocess.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, and both tee and redirecting to a file make the buffer not line-by-line, whereas just running to a tty flushes on every line. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.