python结束无限循环_python如何停止无限while循环并继续执行其余代码?

问题是如果不停止整个程序,我就不能退出while循环。在

当我在我的Raspberry Pi上执行代码时,摄像机开始录制,但是当我想结束视频并按Ctrl+c时,整个程序停止,而不是在while循环之后继续。我以为信号处理器会捕捉到键盘中断,但它没有。在

我的代码:import picamera

import signal

from time import sleep

from subprocess import call

def signal_handler(signal, frame):

global interrupted

interrupted = True

signal.signal(signal.SIGINT, signal_handler)

interrupted = False

# Setup the camera

with picamera.PiCamera() as camera:

camera.resolution = (1640, 922)

camera.framerate = 30

# Start recording

camera.start_recording("pythonVideo.h264")

while True:

sleep(0.5)

print("still recording")

if interrupted:

print("Ctrl+C pressed")

camera.stop_recording()

break

# Stop recording

#camera.stop_recording()

# The camera is now closed.

print("We are going to convert the video.")

# Define the command we want to execute.

command = "MP4Box -add pythonVideo.h264 convertedVideo.mp4 -fps 30"

#Execute command.

call([command], shell=True)

# Video converted.

print("Video converted.")

我尝试了:

^{pr2}$

错误:pi@raspberrypi:~/Documents/useThisFolder $ python cookieVideo.py

still recording

still recording

still recording

still recording

still recording

^Cstill recording

Ctrl+C pressed

Traceback (most recent call last):

File "cookieVideo.py", line 29, in

camera.stop_recording()

File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 1193, in stop_recording

'port %d' % splitter_port)

picamera.exc.PiCameraNotRecording: There is no recording in progress on port 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值