Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping a Python script output continuosly to another program

Tags:

python

unix

pipe

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?

like image 534
dan_s Avatar asked Nov 26 '25 05:11

dan_s


2 Answers

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

like image 125
Padraic Cunningham Avatar answered Nov 28 '25 20:11

Padraic Cunningham


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.

like image 42
PanGalactic Avatar answered Nov 28 '25 21:11

PanGalactic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!