Python-Nonblocking-subprocess
<pre>
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
</pre>
output:
<pre>
<pre>
got output d from fd 3
got output o from fd 3
got output n from fd 3
got output e from fd 3
got output 1 from fd 3
got output
from fd 3
got output d from fd 4
got output o from fd 4
got output n from fd 4
got output e from fd 4
got output 3 from fd 4
got output
from fd 4
got output d from fd 3
got output o from fd 3
got output n from fd 3
got output e from fd 3
got output 2 from fd 3
got output
from fd 3
got output d from fd 4
got output o from fd 4
got output n from fd 4
got output e from fd 4
got output 4 from fd 4
got output
from fd 4
</pre>