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.
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.
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!"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With