I've been reading a lot of threads on this but can't get what I'm looking for working. I'm running a subprocess and output stdout in a pipe. With a while loop I'm then checking multiple parameters while the process runs and exit if certains conditions are met. One of these conditions is if a specific string shows up in stdout. I can check this condition by going through stdout but this seems to freeze the while loop until the subprocess is complete, not checking therefore the other conditions:
cmd=subprocess.Popen(['cmd'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
while True:
stdout_str=''.join(str(i) for i in cmd.stdout.readlines())
if "text" in stdout_str:
break
#check other conditions such as time out, file generated size
if cmd.poll() is not None:
break
time.sleep(.1)
finally:
cmd.kill()
How can I check stdout without blocking the while loop? Thanks!