[Flask] How to Stream and not paralize the server

Pablo Lozano plozano94 at gmail.com
Sun Feb 28 15:04:16 EST 2016


*# all the imports*
*import threading*
*import sqlite3*
*from flask import Flask, request, session, g, redirect, url_for, \*
*         abort, render_template, flash, Response*
*from contextlib import closing*
*import serial*

*from camera import VideoCamera*
*# configuration*
*DATABASE = '/tmp/flaskr.db'*
*DEBUG = True*
*SECRET_KEY = 'development key'*
*USERNAME = 'admin'*
*PASSWORD = 'default'*
*# create our little application :)*
*app = Flask(__name__)*
*app.config.from_envvar('FLASKR_SETTINGS', silent=True)*
*app.config['DEBUG'] = True*
*app.config['SECRET_KEY'] = 'some_really_long_random_string_here'*
*ser = serial.Serial('/dev/ttyACM0', 9600)*
*logged= None*

*@app.route('/command', methods=['GET','POST'])*
*def command():*
*        error=None*
*        global logged*
*        if not logged:*
*                abort(401)*
*        if request.method == 'POST':*
*                message=request.form['command']*
*                message = '&'+str(message)+ '#'*
*                print message*
*                print 'Enviado'*
*                ser.write(message)*
*                #return redirect(url_for('command'))*
*return render_template('command.html', error=error)*

*@app.route('/camera', methods=['GET','POST'])*
*def camera():*
*        return render_template('camera.html')*


*def gen(camara):*
*        while True:*
*                frame = camara.get_frame()*
*                yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' +
frame + b'\r\n\r\n')*

*@app.route('/login', methods=['GET', 'POST'])*
*def login():*
*        error = None*
*        if request.method == 'POST':*
*                if request.form['username'] != USERNAME:*
*                        str(request.form['username'])*
*                        error = 'Invalid username'*
*                elif request.form['password'] != PASSWORD:*

*                    error = 'Invalid password'*
*                else:*
*                        global logged*
*                        logged  = True*
*                        #session['logged_in'] = True*
*                        flash('You were logged in')*
*                        return redirect(url_for('command'))*
*        return render_template('login.html', error=error)*


*@app.route('/logout')*
*def logout():*
*        global logged*
*        logged = None*
*        #session.pop('logged_in', None)*
*        flash('You were logged out')*
*        return redirect(url_for('show_entries'))*
*@app.route('/video_feed')*
*def video_feed():*
*        return Response(gen(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')*

*if __name__ == '__main__':*
*        app.run(host='0.0.0.0',port=4000)*



Okey, this is the code, streaming works, but when I acceed to /camera, it
start streaming and I cant get out of there, because it is blocked o that
while that streams. I want that the server streams, and also that I could
acceed to the /command page while camera continues streaming. Sorry if im
not clear, and thanks for your patience.

2016-02-28 20:55 GMT+01:00 Christophe Bal <projetmbc at gmail.com>:

> Hello.
>
> You should give a minimal example showing the probblem.
>
>
> *Christophe BAL*
> *Enseignant de mathématiques en Lycée **et développeur Python amateur*
> *---*
> *French teacher of **math** in a high school **and **amateur **Python *
> *developer*
>
> 2016-02-28 20:53 GMT+01:00 Pablo Lozano <plozano94 at gmail.com>:
>
>> Hi everybody,
>>
>> I'm making a little server where I have a loging, a page where we stream
>> video, and another page for sending some commands. Steaming video is
>> running this way:
>> http://www.chioka.in/python-live-video-streaming-example/ But when it
>> starts streaming, I cant acceed to the page of commands, because server is
>> bloqued in a while true loop for streaming.
>>
>> I probe launching some threading but then I cant see the camera. What
>> should I do?
>>
>> Thanks everyone.
>>
>> _______________________________________________
>> Flask mailing list
>> Flask at python.org
>> https://mail.python.org/mailman/listinfo/flask
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160228/24c15ed0/attachment.html>


More information about the Flask mailing list