-
-
Save davidbgk/7866f7aefa1da2a6067ed1cd7b284a81 to your computer and use it in GitHub Desktop.
Use named pipes to stream answers between processes without buffering (thanks to stdbuf -o0)
This file contains 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
import os | |
import shlex | |
import subprocess | |
def spawn(cmd): | |
os.mkfifo("toto") | |
with open("toto", "w") as f: | |
process = subprocess.Popen(shlex.split(cmd), stdout=f) | |
process.wait() | |
os.unlink("toto") | |
spawn("stdbuf -o0 python test.py") |
This file contains 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
from __future__ import print_function | |
import os | |
import os.path | |
def read_fifo(filename): | |
with open(filename) as fifo: | |
while os.path.exists(filename): | |
print(fifo.readline(), end='') | |
read_fifo("toto") |
This file contains 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
#! /use/bin/python | |
import time | |
for i in range(100): | |
time.sleep(0.2) | |
print i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment