I'm trying to write a program, which will start and stop for first preview on different POST request. My Code is look like this:

from flask import Flask
import time
import picamera
import logging
import sys
import os
if sys.version_info[0] == 2:
    from cStringIO import StringIO as bio
    from io import BytesIO as bio

app = Flask(__name__)

                    format='%(asctime)s %(levelname)-8s '
                    + '[%(filename)s:%(lineno)s:%(funcName)s()] %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S')

@app.route("/start", methods=['POST'])
def start_preview():
    with picamera.PiCamera() as camera:
        camera.resolution = (1920, 1080)

@app.route("/stop", methods=['POST'])
def stop_preview():
    with picamera.PiCamera() as camera:

if __name__ == "__main__":
    app.run(host='', port='8080')

When I'm trying to start preview, it's works, but the /stop request do nothing. What I'm doing wrong?


I believe you are creating a new instance of PiCamera in your stop_preview function, so the stop_preview isn't acting on the same instance as the start_preview.

Try defining a variable to hold a reference to the PiCamera at the application level. Then use the same variable in both functions.

  • I solved it already. When my code is ready, I will post it here. – Insider Feb 8 '16 at 13:32

protected by goldilocks Dec 27 '17 at 23:32

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.