2
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
  • 10,662
  • 10
  • 51
  • 95
VaFancy
  • 145
  • 2
  • 4
  • 13
  • 7
    Have you tried using `break`...? – 2rs2ts Mar 04 '14 at 21:12
  • 3
    the answer is in the Title – OutFall Mar 04 '14 at 21:14
  • There's no way that this is the only portion of the code. A `KeyboardInterrupt` would trip the `except` and the program would continue after that, but since that's the end of the shown program, it would terminate. – 2rs2ts Mar 04 '14 at 21:41
  • @2rs2ts 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 Mar 04 '14 at 23:49
  • Reading any arbitrary key input is not as straightforward in Python. You could try [this snippet](http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/) I found when googling for "python detect key press." It's up to you to find a solution for that. – 2rs2ts Mar 05 '14 at 15:20

5 Answers5

8

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.

anon582847382
  • 19,907
  • 5
  • 54
  • 57
  • The `while` loop is local to the scope of the `try`, so this will produce a syntax error. – 2rs2ts Mar 04 '14 at 21:38
  • @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. – anon582847382 Mar 04 '14 at 21:44
  • @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 Mar 04 '14 at 23:51
  • @VaFancy you can get any keypress by putting `if msvcrt.kbhit(): break` in your `while` loop instead of a `try`-`except` structure. – anon582847382 Mar 08 '14 at 18:48
  • @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? – VaFancy Mar 08 '14 at 22:01
  • @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 about `curses` so you'll have to do some digging or ask a separate question. – anon582847382 Mar 08 '14 at 22:03
  • @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. – VaFancy Mar 08 '14 at 22:19
4

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!"
nvd
  • 2,995
  • 28
  • 16
2

You could try this:

try:
    while True:
        do_something()
except KeyboardInterrupt:
    pass

Taken from: here

Community
  • 1
  • 1
ederollora
  • 1,165
  • 2
  • 11
  • 29
0

try 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
        except:
            break
Adrian B
  • 1,490
  • 1
  • 19
  • 31
  • 2
    Why add the extra `except`? Silent failures! Arrrgh! Also the `while` is unnecessarily indented. – 2rs2ts Mar 04 '14 at 21:39
  • @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? – VaFancy Mar 05 '14 at 00:04
0

This is the solution I found with threads and standard libraries

Loop keeps going on until one key is pressed
Returns the key pressed as a single character string

Works in Python 2.7 and 3

import thread
import sys

def getch():
    import termios
    import sys, tty
    def _getch():
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(fd)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch
    return _getch()

def input_thread(char):
    char.append(getch())

def do_stuff():
    char = []
    thread.start_new_thread(input_thread, (char,))
    i = 0
    while not char :
        i += 1

    print "i = " + str(i) + " char : " + str(char[0])

do_stuff()
Berni Gf
  • 161
  • 1
  • 5