Interesting.
As Michael suggested this works, mostly:
from time import sleep
def loop():
x = 0
while 1:
print "x:",x
x += 1
sleep(0.5)
if __name__ == "__main__":
while 1:
try:
loop()
except KeyboardInterrupt:
print "Nop
When I do real applications with exception based languages, I almost always
wrap the main function with a try/except block to allow me to gracefully
shut down.
In the case of python, this means 1> Use the main method 2> wrap its
execution in a try catch:
import mymodule
def do_stuff():
pass
Hi. :)
I'm whipping up a program in Python and am having to deal with a user
potentially hitting ctrl-c at any point in the program. I'd like my
Python program to wrap up cleanly when it receives this signal.
I did some Googling and read that Python throws a KeyboardInterrupt
error. What