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.

5
  • 7
    Have you tried using break...? Commented Mar 4, 2014 at 21:12
  • 3
    the answer is in the Title Commented Mar 4, 2014 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. Commented Mar 4, 2014 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? Commented Mar 4, 2014 at 23:49
  • Reading any arbitrary key input is not as straightforward in Python. You could try this snippet I found when googling for "python detect key press." It's up to you to find a solution for that. Commented Mar 5, 2014 at 15:20

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

7 Comments

The while loop is local to the scope of the try, so this will produce a syntax error.
@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.
@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 you can get any keypress by putting if msvcrt.kbhit(): break in your while loop instead of a try-except structure.
@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?
|
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!"

Comments

2

You could try this:

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

Taken from: here

Comments

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

2 Comments

Why add the extra except? Silent failures! Arrrgh! Also the while is unnecessarily indented.
@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?
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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.