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

Update database in the middle of the transaction, possible?

java hibernate jpa transactions

@Transactional public void start() { ... ... int result = entityManager .createQuery("update Users set name=" + value + " where user.id=5").executeUpdate(); ..... }

Best way to mock filewriter

unit-testing groovy metaprogramming

I want to test the following method and specifically that write and close methods were called (In the case of write check what was written was what I expected). The true source code is Java and I am