My python program prints a lot of output. But I often only need the start of it, so I pipe it to head
. How can I change my program to exit once head is done?
example.py:
import time
for i in range(100):
# Print some output
print("x" * 100)
# TODO: Exit here when 'head' is done
while True:
# continue working
time.sleep(1)
Currently, when I run python example.py | head -n10
, I see 10 lines of output in the terminal, but the program keeps running.
How can I exit my python script as soon as head has received all of the output it requires?