Skip to content

Instantly share code, notes, and snippets.

@wesdu
Created May 5, 2014 06:55
Show Gist options
  • Save wesdu/1f6248f8bd7a36038b04 to your computer and use it in GitHub Desktop.
Save wesdu/1f6248f8bd7a36038b04 to your computer and use it in GitHub Desktop.
import subprocess
import select
import os
import sys
import fcntl
p1 = subprocess.Popen("sleep 2 && echo done1 && sleep 5 && echo done2",
shell=True, stdout=subprocess.PIPE)
p2 = subprocess.Popen("sleep 3 && echo done3 && sleep 5 && echo done4",
shell=True, stdout=subprocess.PIPE)
pout = [p1.stdout.fileno(), p2.stdout.fileno()]
for fd in pout:
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
fd1_closed, fd2_closed = 0, 0
while not (fd1_closed and fd2_closed):
inputready, outputready, exceptready = select.select(pout, [], [])
for s in inputready:
got = os.read(s, 1)
if got:
print "got output %s from fd %d" % (got, s)
else:
if s == pout[0]:
fd1_closed = 1
elif s == pout[1]:
fd2_closed = 1
else:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment