1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python2.6
from subprocess import Popen, PIPE
import sys, threading

p1 = Popen(["grep", "-v", "not"], stdin=PIPE, stdout=PIPE)
p2 = Popen(["cut", "-c", "1-10"], stdin=p1.stdout, stdout=PIPE)

def writer(output):
    output.write('hello world\n')
    output.close()
        
def spawn(func, **kwargs):
    t = threading.Thread(target=func, kwargs=kwargs)
    t.start()
    return t

spawn(writer, output=p1.stdin)

for line in p2.stdout:
    print 'RESULT:', repr(line)

This paste never expires. View raw. Pasted through import.