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?


1 Answer 1


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, 2016 at 13:32

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