Does doing a nonblocking read help you out?
import fcntl import os def nonBlockReadline(output): fd = output.fileno() fl = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) try: return output.readline() except: return '' working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) line = nonBlockReadline(working_file.stdout) working_file.stdout.flush() while working_file != "" : print(line) line = nonBlockReadline(working_file.stdout) working_file.stdout.flush()
I'm not sure exactly what you're trying to do, but will this work better? It just reads all the data, instead of reading only one line at a time. It's a little more readable to me.
import fcntl import os def nonBlockRead(output): fd = output.fileno() fl = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) try: return output.read() except: return '' working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) stdout = '' while working_file.poll() is None: stdout += nonBlockRead(working_file.stdout) # we can probably save some time and just print it instead... #print(stdout) stdout = stdout.splitlines() for line in stdout: print(line)
Edit: A generalized script which should be more suited for your use case:
import fcntl import os def nonBlockRead(output): fd = output.fileno() fl = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) try: return output.read() except: return '' working_file = subprocess.Popen(["/pyRoot/iAmACrashyProgram"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) while working_file.poll() is None: stdout = nonBlockRead(working_file.stdout) # make sure it returned data if stdout: # process data working_file.stdin.write(something)