Skip to content

Instantly share code, notes, and snippets.

@kousu
Created July 12, 2014 01:02
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 kousu/4ef4c323e5f7f4d1cab9 to your computer and use it in GitHub Desktop.
Save kousu/4ef4c323e5f7f4d1cab9 to your computer and use it in GitHub Desktop.
nonblocking pipes with Popen
#!/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
#!/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