Python program exiting before signal handler is finished running

I have registered a SIGINT handler to save some data before my python program exits. It seems that the handler is terminated before it finishes. Why is this happening and what can I do about it?

import sys
import signal
import pickle

debug_data = {
    # some hash
}
debug_known_values = {
    # some other hash
}

def exit_run(signal, frame):
    print("Saving debug data...")
    with open("debug_data.pickle", 'wb') as debug_data_file:
       pickle.dump(debug_data, debug_data_file)
       print("done dumping debug data")
    print("Saving debug known values...")
    with open("debug_known_values.pickle", 'wb') as debug_known_values_file:
        pickle.dump(debug_known_values, debug_known_values_file)
        print("done dumping known values")
    print("exiting")
    sys.exit(0)
signal.signal(signal.SIGINT, exit_run)


if __name__ == "__main__":
    input()
    exit_run(None, None)

If I hit Enter to exit the program normally I get

Saving debug data...           
done dumping debug data        
Saving debug known values...   
done dumping known values      
exiting   

If I hit Ctrl + C I get

Saving debug data...            
done dumping debug data         
Saving debug known values...    

I am using python 3.3.2 on Windows x64.

Answers


This problem was caused by using the ComEmu. It was killing the process before it could finish exiting. The code works fine in a normal cmd window.


Need Your Help

SplitViewController app crashes when loading webview

ios ipad sqlite uiwebview uisplitviewcontroller

I am working on an app that uses a splitViewController that loads a html file from a sqlite database. I have an iphone app that does the same thing. It loads a tableview with the database contents ...

Differentiate between Sharepoint User defined columns and default columns

sharepoint sharepoint-2007

I am using SharePoint 2007. How do I differentiate a user created column (the column that a user creates on his own for a SPList) from the columns that are available by default? Any help would be