title: interrupt next-function when iterating large files
body: <p>I'm writing a program which loads data from defferent file-types. For each supported file type I have iterators. So I can go through the data of a file with for example:</p>
  
  <pre><code>it = MyIterator("filename")
  for data in it:
      DoSomethingWithData(data)
  </code></pre>
  
  <p>Now I want to have the possibility to stop this iteration. This is genereally no problem, but in some cases a call to the iterators <code>next()</code>-function takes very very long, because there is no readable data in the file. In those cases the iterator goes through the whole file without finding anything, until it reaches the end and raises StopIteration.</p>
  
  <p>So I want to have the opportunity to stop - or better interrupt - the next-function, without any idea how to do it. I think I have to use something like threads, but I can't kill threads either. Something likethis would be nice:</p>
  
  <pre><code>def Iterate():
      it = MyIterator("filename")
      for data in it:
          self.t0 = time.time()
          DoSomethingWithData(data)
  
  iterationThread = threading.Thread(target=Iterate)
  iterationThread.start()
  
  self.t0 = time.time()
  maxWaitingTime = 10.
  while iterationThread.IsAlive():
      if self.t0 - time.time() &gt; maxWaitingTime:
          KillThread(iterationThread)
  </code></pre>
  
  <p>That's generally what I want to have, but I have no function <code>KillThread</code> of course...</p>
  
  <p>So has anyone an idea how to handle something like that? And it is no possibility to change the iterators itself...</p>