Created
July 12, 2014 01:02
-
-
Save kousu/4ef4c323e5f7f4d1cab9 to your computer and use it in GitHub Desktop.
nonblocking pipes with Popen
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
#!/bin/bash -e | |
i=0 | |
while true; do | |
echo "yadda yadda ||$i||" | |
sleep 3; | |
i=$(($i+1)) | |
done | |
#while true; do #HILARIOUS: | |
# hexdump /dev/urandom | head | |
# echo | |
# sleep 12 | |
#done |
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
#!/usr/bin/env python2 | |
import subprocess | |
p = subprocess.Popen(["./lol.sh"], bufsize=1, stdout=subprocess.PIPE) | |
import time | |
import IPython | |
import os, fcntl | |
flags = fcntl.fcntl(p.stdout, fcntl.F_GETFL) # get current p.stdout flags | |
fcntl.fcntl(p.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) | |
i = 0 | |
while True: | |
try: | |
#print("before block") | |
vvv = p.stdout.read(1023) | |
#print("after sunblock") | |
except IOError, e: | |
#IPython.embed() | |
if e.errno == os.errno.EAGAIN: | |
print("failbox") | |
time.sleep(1) | |
continue | |
else: | |
raise | |
#IPython.embed() | |
#vvv = p.stdout.readline() | |
print i, vvv, "\n" | |
i+=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment