Skip to content

Instantly share code, notes, and snippets.

@drevicko
Forked from zed/try_named_pipe.py
Created June 18, 2014 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drevicko/4fff2f3f002992c6a924 to your computer and use it in GitHub Desktop.
Save drevicko/4fff2f3f002992c6a924 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""Emulate bash:
$ cat /tmp/fifo.tub &
$ gunzip -c /tmp/filedata.dat.gz > /tmp/fifo.tub
http://stackoverflow.com/questions/19859283/python-subprocess-hangs-with-named-pipes
"""
import os
from contextlib import contextmanager
from shutil import rmtree
from subprocess import Popen, check_call
from tempfile import mkdtemp
@contextmanager
def named_pipe():
dirname = mkdtemp()
try:
path = os.path.join(dirname, 'named_pipe')
os.mkfifo(path)
yield path
finally:
rmtree(dirname)
with named_pipe() as path, Popen(["cat", path]), open(path, 'wb') as f:
check_call("gunzip -c /tmp/filedata.dat.gz".split(), stdout=f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment