dan_s
dan_s

Reputation: 471

Piping a Python script output continuosly to another program

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?

Upvotes: 3

Views: 338

Answers (3)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

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

Upvotes: 2

PanGalactic
PanGalactic

Reputation: 72

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.

Upvotes: 1

Prune
Prune

Reputation: 77827

./test2.py | tail -f

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

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

Upvotes: 0

Related Questions