import gzip
from subprocess import Popen, PIPE
from threading import Thread

def f(input, output):
    for line in iter(input.readline, ''):
        output.write(line)
p = Popen(['sort'], bufsize=-1, stdin=PIPE, stdout=PIPE)
Thread(target=f, args=(p.stdout, gzip.open('out.gz', 'wb'))).start()
for s in 'cafebabe':
    p.stdin.write(s + '\n')
p.stdin.close()
подпроцесс python с gzip