I have some program logic that works as follows:
for i in range(10**6):
    foo(i)
    print("foo executed with parameter", i)
    bar(i)
    print("bar executed with parameter", i)
The problem arises when I want to interrupt the loop with Ctrl+C to raise a KeyboardInterrupt.
I make sure that the function bar(i) always runs after the function foo(i); 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.
How is this achievable (with a try...except statement or otherwise) in Python 3?
 
    