How To Interrupt Gracefully In Python Without Losing Your Progress
Here's why everyone is talking about gracefully interrupting Python processes without losing progress...
2 min read
I had a usecase where I was running a loop with each loop doing complex work. I wanted to run the code but also parallel improve it. The code was time consumingly gathering and processing data.
The improvements were future proofing it.
I wanted to kill the long running process in the terminal but not loose any information. So I learnt using ChatGPT how to do this in python.
The idea is:
- Import the
signal
module. - Define a handler function for
SIGINT
that sets a global flag, indicating an interrupt signal was received. - Check this flag at a suitable point in your loop to decide whether to continue or break out of the loop.
This is a neat pattern that is useful for my data engineering work in general where things are happening in iterations and I want to interrupt but not loose the work done. Also, partial failures are painful to deal with so thats a nice thing too.