When an exception occurs, I want to cleanly exit without partially processed data. This is the sort of code that I'm trying:
exit_lock = threading.lock()
def sig_interrupt_handler(signal, frame):
with exit_lock:
sys.exit(0)
signal.signal(signal.SIGINT, sig_interrupt_handler)
while data_left() > 0:
with exit_lock:
data = fetch_data(10)
for datum in data:
processor = Processor(datum)
processor.process()
datum.delete()
However, this code causes my program to lock up when ctrl+c is pressed. I've tried having the interrupt handler spin off a new thread, but this causes ctrl+c to do nothing.
What is the correct way to ensure a clean exit?