检查选择模块
import subprocessimport selectimport timex=subprocess.Popen(['/bin/bash','-c',"while true; do sleep 5; echo yes; done"],stdout=subprocess.PIPE)y=select.poll()y.register(x.stdout,select.POLLIN)while True: if y.poll(1): print x.stdout.readline() else: print "nothing here" time.sleep(1)
编辑:
非posix系统的线程解决方案:
import subprocessfrom threading import Thread import timelinebuffer=[]x=subprocess.Popen(['/bin/bash','-c',"while true; do sleep 5; echo yes; done"],stdout=subprocess.PIPE)def reader(f,buffer): while True: line=f.readline() if line: buffer.append(line) else: breakt=Thread(target=reader,args=(x.stdout,linebuffer))t.daemon=Truet.start()while True: if linebuffer: print linebuffer.pop(0) else: print "nothing here" time.sleep(1)



