I've got a script that runs on a infinite loop and adds things to a database and does things that I can't just stop halfway through so I can't just press ctrl+C and stop it.
I want to be able to somehow stop a while loop but let it finish it's last iteration before it stops.
Let me clarify:
my code looks something like this:
while True:
does something
does more things
does more things
I want to be able to interrupt the while loop at the end, or the beginning, but not between doing things because that would be bad.
and I don't want it to ask me after every iteration if i want to continue.
thanks for the great answers, i'm super grateful but my implementation doesn't seem to be working:
def signal_handler(signal, frame):
global interrupted
interrupted = True
class Crawler():
def __init__(self):
# not relevent
def crawl(self):
interrupted = False
signal.signal(signal.SIGINT, signal_handler)
while True:
doing things
more things
if interrupted:
print("Exiting..")
break
when I press ctr+c the program just keeps going ignoring me