0

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!

tienow
  • 161
  • 10
  • Check this: https://stackoverflow.com/questions/4417546/constantly-print-subprocess-output-while-process-is-running – Nechoj May 04 '22 at 19:33

0 Answers0