title: Finish loop before exiting with a KeyboardInterrupt
body: <p>I have some program logic that works as follows:</p>
  <pre class="lang-py prettyprint-override"><code>for i in range(10**6):
      foo(i)
      print(&quot;foo executed with parameter&quot;, i)
      bar(i)
      print(&quot;bar executed with parameter&quot;, i)
  </code></pre>
  <p>The problem arises when I want to interrupt the loop with Ctrl+C to raise a <code>KeyboardInterrupt</code>.
  I make sure that the function <code>bar(i)</code> always runs after the function <code>foo(i)</code>; i.e. the loop cannot be interrupted between the two function calls (or, if it is, I want to complete the loop before exiting). If the interrupt is received while one of the functions is being executed, I want both to finish before exiting.</p>
  <p>How is this achievable (with a <code>try...except</code> statement or otherwise) in Python 3?</p>