Python 无法使用Flask脚本使Flask应用程序和另一个函数一起运行
我有一个树莓圆周率,我已经设法得到独立工作的独立元素。另一个是使用Flask在我的本地网络上传输摄像头 然而,我希望它们同时运行,我一直在研究Flask脚本模块和管理器功能。我相信我的设置是正确的。但是,当我运行“runserver”命令时,实际上什么都没有发生 我想用Flask脚本做的事情可能吗?如果我的方法是正确的,或者什么是更好的解决方案 谢谢 以下是迄今为止我的python脚本:Python 无法使用Flask脚本使Flask应用程序和另一个函数一起运行,python,python-3.x,flask,raspberry-pi3,flask-script,Python,Python 3.x,Flask,Raspberry Pi3,Flask Script,我有一个树莓圆周率,我已经设法得到独立工作的独立元素。另一个是使用Flask在我的本地网络上传输摄像头 然而,我希望它们同时运行,我一直在研究Flask脚本模块和管理器功能。我相信我的设置是正确的。但是,当我运行“runserver”命令时,实际上什么都没有发生 我想用Flask脚本做的事情可能吗?如果我的方法是正确的,或者什么是更好的解决方案 谢谢 以下是迄今为止我的python脚本: from importlib import import_module import os from fla
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)
gpiozero.MotionSensor在运行不同线程中的代码时具有事件
因此,使用pir=MotionSensor(4)
及
pir.when\u motion=MakeSnapshot
当然,您需要定义函数MakeSnapshot()
阅读有关该类的更多信息:所以您启动了服务器,但如果我理解正确,您将进入sensmotion(),它将永远被卡住,永远无法到达app.run()。您需要异步并行执行。尝试使用一些任务管理器,如芹菜或线程-谢谢你的回复,我会看看你的建议。