In python, How do I check the stdout from a subprocess.Popen object for anything to read? I'm writing a wrapper around a tool that sometimes runs for hours on-end. Using .readline() on the stdout from the child process is severely cutting in to the speed of the script when run for longer than a few minutes. I need a way to check the stdout more efficiently if there's anything to read. By the way, this particular tool only writes complete lines at a time. The script goes like this:
#!/usr/bin/python -u
#thiswrap.py
import sys, time
from subprocess import *
chldp = Popen(sys.argv[1], bufsize=0, stdout=PIPE, close_fds=True)
chstdin,chstdout=chldp.stdin,chldp.stdout
startnoti=False
while not chldp.poll():
rrl=chstdout.readline() # <--- this is where the problem is
if rrl[-8:]=='REDACTED TEXT':
sys.stdout.write(rrl[:-1]+' \r')
if not startnoti: startnoti=True
else:
if startnoti: sys.stdout.write('\n')
sys.stdout.write(rrl)
if startnoti: # REDACTED
time.sleep(0.1)
time.sleep(0.1)
Any ideas?