How to break this loop in Python by detecting key press
34,489
Solution 1
You want your code to be more like this:
from subprocess import call
while True:
try:
call(["raspivid -n -b 2666666.67 -t 5000 -o test.mp4"], shell=True)
call(["raspivid -n -b 2666666.67 -t 5000 -o test1.mp4"], shell=True)
except KeyboardInterrupt:
break # The answer was in the question!
You break
a loop exactly how you would expect.
Solution 2
Use a different thread to listen for a "ch".
import sys
import thread
import tty
import termios
from time import sleep
breakNow = False
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def waitForKeyPress():
global breakNow
while True:
ch = getch()
if ch == "b": # Or skip this check and just break
breakNow = True
break
thread.start_new_thread(waitForKeyPress, ())
print "That moment when I will break away!"
while breakNow == False:
sys.stdout.write("Do it now!\r\n")
sleep(1)
print "Finally!"
Author by
VaFancy
Updated on May 07, 2020Comments
-
VaFancy about 4 years
from subprocess import call try: while True: call (["raspivid -n -b 2666666.67 -t 5000 -o test.mp4"],shell=True) call (["raspivid -n -b 2666666.67 -t 5000 -o test1.mp4"],shell=True) except KeyboardInterrupt: pass
I plan to make it breaking loop while I am pressing any button. However I tried lots of methods to break the and none of them worked.
-
2rs2ts about 10 yearsThe
while
loop is local to the scope of thetry
, so this will produce a syntax error. -
2rs2ts about 10 yearsWhy add the extra
except
? Silent failures! Arrrgh! Also thewhile
is unnecessarily indented. -
anon582847382 about 10 years@2rs2ts Thank you very much, I failed to look at the way I would implement this and not just the keyword. Answer has been edited.
-
VaFancy about 10 years@AlexThomton Thank you for answering me. This is the latest version of my code. I saw it on someone's blog. Anyway, these methods can only break the loop while I am pressing control+c. What I am trying to do is breaking the loop by pressing any buttons. By the way, as you see, my code aims to record the video again and again until I press a button. Is that possible to stop recording immediately after pressing?
-
VaFancy about 10 years@AdrianB Thank you for answering me. It works as same as using one except. Are there any methods that can break a loop by pressing any keys?
-
anon582847382 about 10 years@VaFancy you can get any keypress by putting
if msvcrt.kbhit(): break
in yourwhile
loop instead of atry
-except
structure. -
VaFancy about 10 years@AlexThomton Many thanks. But as far as I know 'msvcrt' can only work on windows. I need the method that can working on Raspberry PI, is there any?
-
anon582847382 about 10 years@VaFancy In that case, on a UNIX based system such as that, use the
curses
module. However I'm afraid that's where my help ends, as I am a Windows user I know next to nothing aboutcurses
so you'll have to do some digging or ask a separate question. -
VaFancy about 10 years@AlexThornton You have already helped me a lot and I appreciate it. I will try to find out how to solve this. BTW, it seems that I spelled your name incorrectly all the time.