title: How to retry iterator next() after exception
body: <p>I have an iterator that sometimes gives me a socket timeout, and I'd like to know how to retry on socket timeout instead of skipping ahead to the next item in the iterator.</p>
  
  <p>The iterator is created using: 
  <code>iterator = pool.imap(process_hit, new_nongeneric_search.scan())</code>
  So I'm calling a function (<code>process_hit</code>) on each item in the results of an Elasticsearch query (<code>new_nongeneric_search.scan()</code>). Currently when I encounter a timeout, I just catch the exception and move on to calling <code>next(it)</code> again.</p>
  
  <p>This is my current approach: </p>
  
  <pre><code>while True:
      try:
          next(it)
      except StopIteration:
          logging.info("No more results to process.")
          return
      except socket.timeout:
          logging.warning("Encountered timeout.")
  </code></pre>
  
  <p>If I retry the same item enough times by rerunning the script manually, the timeout will resolve itself, so I'd like to be able to retry the problematic item programmatically instead of just repeating the search multiple times.</p>