Created
September 12, 2016 21:09
-
-
Save eenblam/7ffe305b298a97cfbd9078fb28b7fdda to your computer and use it in GitHub Desktop.
Generating and consuming infinite unix pipes with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import fileinput | |
import sys | |
# Reverse each incoming line | |
input_lines = fileinput.input(sys.argv[2:]) | |
for line in input_lines: | |
line = line.strip()[::-1] + '\n' | |
sys.stdout.write(line) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from sys import stdout | |
from time import sleep | |
# Print an integer roughly once every second | |
i = 0 | |
while(True): | |
stdout.write(str(i) + '\n') | |
i += 1 | |
sleep(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
stdbuf -o0 python factory.py | python consumer.py - | |
# Depending on your system, you may instead need this: | |
#unbuffer python factory.py | python consumer.py - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment