title: python subprocess popen with timeout and or kill
body: <p>I am attempting to grep a continuous stream on the server monitor via popen for 5 seconds, capture the output and perform some operations on it. I am able to do it till here but the subprocess opened by Popen is staying back and continuously running (as it's a monitor stream) and affecting my CPU stats. Below is what I am using to run.</p>
  
  <pre><code>class ThreadWorker(threading.Thread):
  def __init__(self, callable, *args, **kwargs):
      super(ThreadWorker, self).__init__()
      self.callable = callable
      self.args = args
      self.kwargs = kwargs
      self.setDaemon(True)
  
  def run(self):
      try:
          self.callable(*self.args, **self.kwargs)
      #except wx.PyDeadObjectError:
          #pass
      except Exception, e:
          print e
  
  if __name__ == "__main__":
  
  def worker(pipe,fileName,grabtime):
      f = csv.writer(open(fname,'wb'),delimiter='\t',quoting=csv.QUOTE_ALL)
  
      while True:
          line = pipe.readline()
          csvlist = [line.rstrip(),int(grabtime)]
          f.writerow(csvlist)
  
  
  fname = 'test.csv'
  
  proc = Popen(mycmdhere, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
  
  try:
  stdout_worker = ThreadWorker(worker, proc.stdout, fname,time.time())
  stdout_worker.start()
  stdout_worker.join(5)
  except Exception, msg:
      print "Timed out!"
  </code></pre>
  
  <p>That's my code. Any help in trying to kill the continuous stream will be great.</p>