-
For several applications, you might want to stop a while by pressing a key. It can be useful for example to prevent
Ctrl-C
a script from happening (which can be problematic for closing opened files, ...). One possibility might be to stop a loop just by pressing a key. Sadly, there are no direct way to do that in python.Solution with try/except
One solution could be to use exceptions and to include your whole program into a try (snippet found here):
from time import sleep def do_things(): try: while 1: print "in loop" sleep(1) except KeyboardInterrupt: print "loop is done" if __name__ == "__main__": do_things()
The program will try to run the
while 1:
until it sees a keyboard interruption (in our case, it's aCtrl-C
). In that case, instead of quitting the script, it will run theexcept KeyboardInterrupt:
part. Despite that this solution works, it needsCtrl-C
to stop the loop and force yourself to put your whole script into atry
.Solution with threads
Another solution would be to use the
raw_input
from python which awaits te user to pressEnter
. In order to prevent the waiting part, a solution found here could be to use a different thread for the key pressing:import thread from time import sleep def input_thread(continue_list): raw_input() continue_list[0] = False def do_things(): continue_list = [True] thread.start_new_thread(input_thread, (continue_list,)) while continue_list[0]: print "in loop" sleep(1) print "loop is done" if __name__ == "__main__": do_things()
In that case, a new thread will be started (called
input_thread
) and will be given the variablecontinue_list
which is a ... list! Therefore, the originalcontinue_list
and the threaded one will both be pointing at the same memory adresscontinue_list[0]
(initialized atTrue
). The loop fromdo_things
will keep running untilcontinue_list[0]
is modified toFalse
. This can only happen in the threaded part, which will be blocked (waiting) at theraw_input()
until the user pressEnter
. In that case,continue_list[0]
will be changed (in the threadedinput_thread
and indo_things()
) and will exit the loop. Despite that only theEnter
key works, it found this solution rather nice and elegant compared to the previous one. In order to take another key as input, you might need to load some specific librairy for that.Note: The previous link also includes a solution based on opencv. Despite that it works nicely and with any desired key, it still requires the use of the opencv librairy, which can be heavy for such a small task.
Please register or sign in to comment