离开页面时停止线程运行

时间:2015-01-11 18:05:47

标签: python multithreading flask raspberry-pi

我正在使用this个人代码,因此我可以从我的PiCamera流式传输视频

camera_pi.py:

import time
import io
import threading
import picamera


class Camera(object):
    thread = None  # background thread that reads frames from camera
    frame = None  # current frame is stored here by background thread

    def __init__(self):
        if self.thread is None:
            # start background frame thread
            self.thread = threading.Thread(target=self._thread)
            self.thread.start()

            # wait until frames start to be available
            while self.frame is None:
                time.sleep(0)

    def get_frame(self):
        return self.frame

    @classmethod
    def _thread(cls):
        with picamera.PiCamera() as camera:
            # camera setup
            camera.resolution = (1280, 720)
            camera.hflip = False
            camera.vflip = False

            # let camera warm up
            camera.start_preview()
            time.sleep(2)

            stream = io.BytesIO()
            for foo in camera.capture_continuous(stream, 'jpeg',
                                                 use_video_port=True):
                # store frame
                stream.seek(0)
                cls.frame = stream.read()

                # reset stream for next frame
                stream.seek(0)
                stream.truncate()

Main Flask App(这是我的代码的一部分:

from camera_pi import Camera
@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(gen(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

def gen(camera):
    """Video streaming generator function."""
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

Stream.html:

<div class="panel panel-default">
  <div class="panel-heading">
    <h1 class="panel-title">Live Streaming</h1>
  </div>
  <div class="panel-body">
    <img id="pic" src="{{ url_for('video_feed') }}" alt="live stream link" class="img-responsive img-rounded"></img>
  </div>
</div>

我的整个项目工作正常,直到我渲染stream.html页面并调用流功能。当你实际加载另一个页面时,似乎流线程仍在正常运行? 离开stream.html页面时有没有办法杀死线程?离开stream.html意味着您不再流式传输,因此不需要运行该线程。原因是没有理由杀死我的记忆。

1 个答案:

答案 0 :(得分:1)

不支持杀死线程。只需在线程的循环中添加一个全局标志,例如:

        for foo in camera.capture_continuous(stream, 'jpeg',
                                             use_video_port=True):
            if stop_the_thread: break

(并在循环之后做正确关闭相机所需的任何事情,如果有的话)。

在主代码中,在开始时将全局stop_the_thread设置为False,然后在确定线程必须停止时设置为True

在这种特定情况下,使用类属性cls.stop_the_thread而不是实际的全局变量会更优雅,但这不会影响关键概念。