Cant get Flask app and another function to run together using Flask Script
I've got a raspberry pi and I've managed to get to separate elements working independently. On to sense motion and take a picture, another to stream the cameras feed over my local network using Flask.
However i want these to run simultaneously, I've been looking at the Flask Script module and the Manger functionality. I believe i have it setup correctly. However when I run the 'runserver' command nothing actually happens.
Is what I want to do even possible with Flask Script? If it is is my approach correct, or what would be a better solution.
Thanks
Here's is my python script so far:
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'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rn')
@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
add a comment |
I've got a raspberry pi and I've managed to get to separate elements working independently. On to sense motion and take a picture, another to stream the cameras feed over my local network using Flask.
However i want these to run simultaneously, I've been looking at the Flask Script module and the Manger functionality. I believe i have it setup correctly. However when I run the 'runserver' command nothing actually happens.
Is what I want to do even possible with Flask Script? If it is is my approach correct, or what would be a better solution.
Thanks
Here's is my python script so far:
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'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rn')
@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
So you launch server, but if I understand it correctly you are going to senseMotion() where it is stuck forever and never reaches app.run(). You need asynchronous parallel executions. Try to use some task manager like Celery - stackoverflow.com/questions/14588253/… or Threads - stackoverflow.com/questions/36617859/…
– omdv
Nov 17 at 13:41
Thanks for your reply, I'll have a look at your suggestions.
– Pedroson
Nov 17 at 17:54
add a comment |
I've got a raspberry pi and I've managed to get to separate elements working independently. On to sense motion and take a picture, another to stream the cameras feed over my local network using Flask.
However i want these to run simultaneously, I've been looking at the Flask Script module and the Manger functionality. I believe i have it setup correctly. However when I run the 'runserver' command nothing actually happens.
Is what I want to do even possible with Flask Script? If it is is my approach correct, or what would be a better solution.
Thanks
Here's is my python script so far:
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'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rn')
@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
I've got a raspberry pi and I've managed to get to separate elements working independently. On to sense motion and take a picture, another to stream the cameras feed over my local network using Flask.
However i want these to run simultaneously, I've been looking at the Flask Script module and the Manger functionality. I believe i have it setup correctly. However when I run the 'runserver' command nothing actually happens.
Is what I want to do even possible with Flask Script? If it is is my approach correct, or what would be a better solution.
Thanks
Here's is my python script so far:
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'--framern'
b'Content-Type: image/jpegrnrn' + frame + b'rn')
@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
python python-3.x flask raspberry-pi3 flask-script
asked Nov 17 at 13:27
Pedroson
596
596
1
So you launch server, but if I understand it correctly you are going to senseMotion() where it is stuck forever and never reaches app.run(). You need asynchronous parallel executions. Try to use some task manager like Celery - stackoverflow.com/questions/14588253/… or Threads - stackoverflow.com/questions/36617859/…
– omdv
Nov 17 at 13:41
Thanks for your reply, I'll have a look at your suggestions.
– Pedroson
Nov 17 at 17:54
add a comment |
1
So you launch server, but if I understand it correctly you are going to senseMotion() where it is stuck forever and never reaches app.run(). You need asynchronous parallel executions. Try to use some task manager like Celery - stackoverflow.com/questions/14588253/… or Threads - stackoverflow.com/questions/36617859/…
– omdv
Nov 17 at 13:41
Thanks for your reply, I'll have a look at your suggestions.
– Pedroson
Nov 17 at 17:54
1
1
So you launch server, but if I understand it correctly you are going to senseMotion() where it is stuck forever and never reaches app.run(). You need asynchronous parallel executions. Try to use some task manager like Celery - stackoverflow.com/questions/14588253/… or Threads - stackoverflow.com/questions/36617859/…
– omdv
Nov 17 at 13:41
So you launch server, but if I understand it correctly you are going to senseMotion() where it is stuck forever and never reaches app.run(). You need asynchronous parallel executions. Try to use some task manager like Celery - stackoverflow.com/questions/14588253/… or Threads - stackoverflow.com/questions/36617859/…
– omdv
Nov 17 at 13:41
Thanks for your reply, I'll have a look at your suggestions.
– Pedroson
Nov 17 at 17:54
Thanks for your reply, I'll have a look at your suggestions.
– Pedroson
Nov 17 at 17:54
add a comment |
1 Answer
1
active
oldest
votes
gpiozero.MotionSensor has event when_motion
which runs code in different thread.
so use pir = MotionSensor(4)
and
pir.when_motion = MakeSnapshot
Of course you need to define function MakeSnapshot()
Read more about this class: https://gpiozero.readthedocs.io/en/stable/api_input.html#motion-sensor-d-sun-pir
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351688%2fcant-get-flask-app-and-another-function-to-run-together-using-flask-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
gpiozero.MotionSensor has event when_motion
which runs code in different thread.
so use pir = MotionSensor(4)
and
pir.when_motion = MakeSnapshot
Of course you need to define function MakeSnapshot()
Read more about this class: https://gpiozero.readthedocs.io/en/stable/api_input.html#motion-sensor-d-sun-pir
add a comment |
gpiozero.MotionSensor has event when_motion
which runs code in different thread.
so use pir = MotionSensor(4)
and
pir.when_motion = MakeSnapshot
Of course you need to define function MakeSnapshot()
Read more about this class: https://gpiozero.readthedocs.io/en/stable/api_input.html#motion-sensor-d-sun-pir
add a comment |
gpiozero.MotionSensor has event when_motion
which runs code in different thread.
so use pir = MotionSensor(4)
and
pir.when_motion = MakeSnapshot
Of course you need to define function MakeSnapshot()
Read more about this class: https://gpiozero.readthedocs.io/en/stable/api_input.html#motion-sensor-d-sun-pir
gpiozero.MotionSensor has event when_motion
which runs code in different thread.
so use pir = MotionSensor(4)
and
pir.when_motion = MakeSnapshot
Of course you need to define function MakeSnapshot()
Read more about this class: https://gpiozero.readthedocs.io/en/stable/api_input.html#motion-sensor-d-sun-pir
answered Nov 20 at 5:52
Koxo
694
694
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53351688%2fcant-get-flask-app-and-another-function-to-run-together-using-flask-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
So you launch server, but if I understand it correctly you are going to senseMotion() where it is stuck forever and never reaches app.run(). You need asynchronous parallel executions. Try to use some task manager like Celery - stackoverflow.com/questions/14588253/… or Threads - stackoverflow.com/questions/36617859/…
– omdv
Nov 17 at 13:41
Thanks for your reply, I'll have a look at your suggestions.
– Pedroson
Nov 17 at 17:54