You can try wrapping that code in a try/except block, because keyboard interrupts are just exceptions:

try:
    while True:
        IDs2=UpdatePoints(value,IDs2)
        time.sleep(10)
except KeyboardInterrupt:
    print('interrupted!')

Then you can exit the loop with CTRL-C.

Answer from Jess on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how can i implement a keyboard interrupt in a while loop to break it instantly regardless of the current execution stage?
r/learnpython on Reddit: How can I implement a keyboard interrupt in a while loop to break it instantly regardless of the current execution stage?
July 12, 2023 -

If I have a while loop, lest say this one:

while True:
print("a")
time.sleep(3)
if keyboard.is_pressed('q'):
    break

The code only stops when I press 'q' after the previous code is finished. Is there a way to modify the loop so that it can be instantly interrupted regardless of which part of the code is being executed? I want the loop to break immediately upon pressing 'q' without waiting for the sleep period to finish.

Thanks in advance!

What is KeyboardInterrupt? May 10, 2020
r/learnpython
6y ago
Is there a way to interrupt input() [Python3] May 26, 2016
r/learnpython
10y ago
How can I add an keyboard interrupt to a program? Dec 3, 2018
r/learnpython
7y ago
"KeyboardInterrupt" Dec 6, 2024
r/learnpython
last yr.
More results from reddit.com
Discussions

Unable to quit infinite while loop in idle 3.12.0 (64 bit) with keyboard interrupt
Hello I am learning python, in IDLE when i write # infinite while loop i = 1 while i More on discuss.python.org
🌐 discuss.python.org
4
0
December 10, 2023
Python: how to interrupt, then return to while loop, without goto? - Stack Overflow
I'm running a simple PID control program in python. Basically an infinite while loop, which reads from sensors then calculates the appropriate control signal, as well as outputs diagnostic info to ... More on stackoverflow.com
🌐 stackoverflow.com
October 22, 2015
python - How to interrupt a while loop with a function - Stack Overflow
ok so this is probably so easy but i am trying to find a away for the past 4 hours and i can't find it. So i have this discord bot where is like a stopwatch, you enter a command and the stopwatch s... More on stackoverflow.com
🌐 stackoverflow.com
August 18, 2022
python - How to interrupt a while loop, run a function and go back to the loop - Stack Overflow
I'm beginning with Python and Raspberry Pi and was trying to make some Physical Computing with them. I began with that: https://www.raspberrypi.org/learning/physical-computing-with-python/ and More on stackoverflow.com
🌐 stackoverflow.com
September 18, 2017
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
July 10, 2026 - Getting back to the example in ... (most recent call last): ... KeyboardInterrupt · To terminate this code, you have to press Ctrl+C, which interrupts the program’s execution from the keyboard....
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 202945 › interrupt-a-while-loop
python - Interrupt a while loop | DaniWeb
July 12, 2009 - Just watch out that your break isn't inside any other while or for loops inside the overall while loop, as it breaks out of the deepest-nested loop that it's in.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
button interrupt while loop exit - Python
It's kind of hard to figure out how to keep track on status and at the same time exit the while loop. More details on you advice would be appreciated. ... With out seeing the rest of your code its difficult to be sure this will work. but try this. ... loopcontrol = 0 led2stat = False but1stat = False def log_gps(channel): global but1stat, led2stat, loopcontrol if but1stat == False and led2stat == False: GPIO.output(led2,True) loopcontrol = 1 led2stat = True if but1stat == False and led2stat == True: GPIO.output(led2,False) print("STOP GPS LOG") led2stat = False loopcontrol = 0 GPIO.add_event_detect(but1, GPIO.FALLING, callback=log_gps, bouncetime=350) and move the while loop in to the main part of your code .
🌐
Pierian Training
pieriantraining.com › home › python tutorial: how to stop an infinite loop in python
Python Tutorial: How to stop an infinite loop in Python - Pierian Training
April 12, 2023 - One of the simplest ways to stop an infinite loop in Python is by using a keyboard interrupt. This method involves pressing the “Ctrl + C” keys on your keyboard while the program is running.
Find elsewhere
🌐
LabEx
labex.io › tutorials › python-how-to-manage-python-loop-interruptions-419934
How to manage Python loop interruptions | LabEx
## Example of continue in a while loop count = 0 while count < 5: count += 1 if count == 3: continue print(count) ## Prints 1, 2, 4, 5 ... When learning loop control, practice is key. LabEx provides interactive Python environments to experiment with these concepts hands-on. Python offers multiple techniques to manage and interrupt loop execution, providing developers with flexible control over iterative processes.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python while Loop (Infinite Loop, break, continue) | note.nkmk.me
August 18, 2023 - Basic syntax of while loops in ... loop with while statement: while True: Stop the infinite loop using keyboard interrupt (ctrl + c) Forced termination ·...
🌐
Finxter
blog.finxter.com › home › learn python blog › how to kill a while loop with a keystroke in python?
How to Kill a While Loop with a Keystroke in Python? - Be on the Right Side of Change
June 9, 2022 - To end a while loop prematurely in Python, press CTRL-C while your program is stuck in the loop. This will raise a KeyboardInterrupt error that terminates the whole program. To avoid termination, enclose the while loop in a try/except block ...
🌐
Stack Overflow
stackoverflow.com › questions › 40573860 › how-to-interrupt-a-while-loop-run-a-function-and-go-back-to-the-loop
python - How to interrupt a while loop, run a function and go back to the loop - Stack Overflow
September 18, 2017 - put the checking for pedestrian_press var at my_sleep function so it will get check everytime my_sleep being call in while loop that make it almost ~1s delay interrupt
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-kill-a-while-loop-with-a-keystroke-in-python
How to Kill a While Loop with a Keystroke in Python? - GeeksforGeeks
July 23, 2025 - This method allows you to manually stop the loop by pressing Ctrl+C. Python · import time try: num = 11 while True: if num % 2 == 0: break print(num) num = num + 2 time.sleep(1) # Wait 1 second before next iteration except KeyboardInterrupt: ...
🌐
Real Python
realpython.com › lessons › interrupting-loop-iteration
Interrupting Loop Iteration (Video) – Real Python
00:10 If we use break, we’re immediately going to terminate the loop entirely, so I’m actually going to just jump down to the last statement after the while loop.
Published: March 1, 2019
🌐
freeCodeCamp
freecodecamp.org › news › python-while-loop-tutorial
Python While Loop Tutorial – While True Syntax Examples and Infinite Loops
November 13, 2020 - The first line defines a while True loop that will run indefinitely until a break statement is found (or until it is interrupted with CTRL + C).
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How To Use break, continue, and pass Statements in Python | DigitalOcean
Learn how to break out of nested loops in Python, exit specific loop levels using flags, functions, exceptions, and best practices for clean loop control
🌐
Reddit
reddit.com › r/learnpython › interrupt and event loops in python.
r/learnpython on Reddit: Interrupt and event loops in python.
June 12, 2023 -

Hey!

I wanna do the following thing in Python and I'm not very familiar with the multithreading in it.

Basically, I want a separate thread for an event loop that can both a) step on demand b) wait indefinitely and continue on demand c) exit on demand.

With pseudo code:

My main program would look like this

worker = MyClassThatDoesSomeShit()
t = Thread(target=function_that_can_do_what_i_want, args=(worker,))

while True:
    text = input()
    if text == "step":
        t.interrupt_and_skip_to_next_step()
    elif text == "wait":
        t.interrupt_and_wait_for_continuation()
    elif text == "continue":
        t.step_and_continue_event_loop()
    elif text == "exit":
        t.terminate action()

And the other half could look something like this:

while not terminated:
    try:
        worker.do_something()
        sleep(some seconds)
    except InterruptedException of type "step":
        pass
    except InterruptedException of type "wait":
        try:
            sleep(forever)
        except InterruptedException of type "continue":
            pass
    except InterruptedException of type "exit":
        break

I would just need a bit of help, because I don't know how this all works in Python.

Thank you!