3

I want to output some data from a Python script continuously to another program. As an example I will use cat, this is what currently happens:

If my test1.py script is like this:

print("Hello!")

when I run ./test1.py | cat the output is Hello!, it works because the script terminates immediately after execution.

The problem occurs when I have a script that writes continuously and never terminates like test2.py:

import time

a = 0
while True:
  a += 1
  print(a)
  time.sleep(1)

Then ./test2.py | cat just hangs there because the script is not terminating.

I would like to send one number every second to cat and display it in real time, is it possible?

3 Answers 3

2

set flush=True in your print, your output is getting buffered, there is a nice artice Unix buffering delays output to stdout, ruins your day that explains what is going on:

import time

a = 0
while True:
  a += 1
  print(a, flush=True)
  time.sleep(1)

If you are using python2 add from __future__ import print_function

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

2 Comments

This fixed my problem, I will mark this one as the correct answer because it does not require to directly use sys.stdout, it is also nice to know about the new Python3 features.
@dan_s, no worries, I added a link to an article that gives a nice explanation
1

You need to flush to the stdout after printing. So your code will look like this:

import sys
import time

a = 0
while True:
    a += 1
    print(a)
    time.sleep(1)
    sys.stdout.flush()

Now running python script.py | cat will print a.

Comments

0

./test2.py | tail -f

-f, --follow[={name|descriptor}]

      output  appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent

Comments

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.