title: Check a condition while running subprocess
body: <p>I am executing a command using subprocess.Popen(). I want to wait for the process to finish before executing the rest of my code but at the same time I would like to check the status of the produced files after 2 min of running the subprocess. If the size of file is zero then I want to stop the procedure. Currently my code is as below. Is there any smarter way to do this?</p>
  
  <pre><code>  def execute(command,outputfilename):
      process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
      start_time=time.time()
      Is_Working=False
      while True:
       process.poll()
       if not Is_Working:
           #allow 2 minutes for the process to create results
           if process.returncode == None and (time.time()-start_time)//60&gt;1:
               Is_Working=True
               if (os.stat(outputfilename)[6]==0):
                   process.kill()
                   return
       if process.returncode != None: 
           break
  
      output, errors=process.communicate()
  </code></pre>