Skip to content

Instantly share code, notes, and snippets.

@eenblam
Created September 12, 2016 21:09
Show Gist options
  • Save eenblam/7ffe305b298a97cfbd9078fb28b7fdda to your computer and use it in GitHub Desktop.
Save eenblam/7ffe305b298a97cfbd9078fb28b7fdda to your computer and use it in GitHub Desktop.
Generating and consuming infinite unix pipes with Python
#!/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)
#!/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)
#!/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