【问题标题】:Cant get Flask app and another function to run together using Flask Script无法使用 Flask 脚本让 Flask 应用程序和另一个函数一起运行
【发布时间】:2019-04-20 11:00:17
【问题描述】:

我有一个树莓派,我已经设法让独立的元素独立工作。一个是感知运动并拍照,另一个是使用 Flask 在我的本地网络上流式传输相机馈送。

但是我希望它们同时运行,我一直在研究 Flask 脚本模块和 Manger 功能。我相信我的设置正确。但是,当我运行“runserver”命令时,实际上什么也没发生。

我想用 Flask Script 做些什么?如果这是我的方法是正确的,或者什么是更好的解决方案。

谢谢

到目前为止,这是我的 python 脚本:

from importlib import import_module
import os
from flask_script import Server, Manager
from flask import Flask, render_template, Response
from camera import Camera
from gpiozero import MotionSensor
import time

# Code to take a snapshot when motion is detected
def senseMotion():
    pir = MotionSensor(4)

    while True:
        if pir.motion_detected:
            print('Motion detected')
            Camera.snapshot()
            time.sleep(60)

app = Flask(__name__)
manager = Manager(app)


# Flask app to view video stream in browser
@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


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')


@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')

@manager.command
def runserver():
    senseMotion()
    app.run(host='0.0.0.0', threaded=True)


if __name__ == "__main__":
    manager.run()

#if __name__ == '__main__':
    #app.run(host='0.0.0.0', threaded=True)

【问题讨论】:

标签: python python-3.x flask raspberry-pi3 flask-script


【解决方案1】:

gpiozero.MotionSensor 有事件when_motion 在不同的线程中运行代码。

所以使用pir = MotionSensor(4)

pir.when_motion = MakeSnapshot

当然你需要定义函数MakeSnapshot()

阅读有关本课程的更多信息:https://gpiozero.readthedocs.io/en/stable/api_input.html#motion-sensor-d-sun-pir

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多