KeyboardInterrupt exception -- thrown when Ctrl-C, or Ctrl-Break on some machines, are pressed in a Python console window -- is commonly used to gracefully break out of long-process loops, allowing your script to perform cleanup, logging etc, before exiting.
Under my ArcGIS 9.3.1 SP2/Windows XP SP3 environment, there is an issue with catching the KeyboardInterrupt exception if the
arcgisscripting module is loaded. Instead of hitting the KeyboardInterrupt catch statement, the script aborts abruptly, spitting out the following message:
forrtl: error (200): program aborting due to control-C event
If the arcgisscripting module is not loaded (removed from the
import statement), the KeyboardInterrupt exception is thrown and processed properly.
From what I've read this is due to the use of Fortran in the arcgisscripting module which somehow overrides Python's interrupt processing.
Here's an example script you can use to test and reproduce the issue. Save it as a Python script, run it from the Windows command prompt and press Ctrl-C (Ctrl-Break on some machines) to test whether the KeyboardInterrupt exception is processed. Simply toggle the commenting for the
import arcgisscripting line.
#import arcgisscripting import time try: while(True): time.sleep(1) except KeyboardInterrupt: print "KeyboardInterrupt detected!"
Anyone know of a workaround to this problem? Thanks in advance for any insights you may have.
edit: here is my test script using Jason's suggestion which unfortunately doesn't work:
import arcgisscripting import signal import time def ctrlc(sig, frame): raise KeyboardInterrupt("CTRL-C!") print "Waiting 65s ..." time.sleep(65) print "Installing SIGINT handler" signal.signal(signal.SIGINT, ctrlc) try: while(True): time.sleep(1) except KeyboardInterrupt: print "KeyboardInterrupt detected!"