streaming shell output from flask view works, but never ends

  • Last Update :
  • Techknowledgy :

Instead, either write

for line in iter(proc.stdout.readline, b ''):

or, even better:

for line in proc.stdout:

Suggestion : 2

This is a basic view function that generates a lot of CSV data on the fly. The trick is to have an inner function that uses a generator to generate data and to then invoke that function and pass it to a response object:,Note that when you stream data, the request context is already gone the moment the function executes. Flask 0.9 provides you with a helper that can keep the request context around during the execution of the generator:,Sometimes you want to send an enormous amount of data to the client, much more than you want to keep in memory. When you are generating the data on the fly though, how do you send that back to the client without the roundtrip to the filesystem?,Each yield expression is directly sent to the browser. Note though that some WSGI middlewares might break streaming, so be careful there in debug environments with profilers and other things you might have enabled.

@app.route('/large.csv')
def generate_large_csv():
   def generate():
   for row in iter_all_rows():
   yield f "{','.join(row)}\n"
return app.response_class(generate(), mimetype = 'text/csv')
def stream_template(template_name, ** context):
   app.update_template_context(context)
t = app.jinja_env.get_template(template_name)
rv = t.stream(context)
rv.enable_buffering(5)
return rv

@app.route('/my-large-page.html')
def render_large_template():
   rows = iter_all_rows()
return app.response_class(stream_template('the_template.html', rows = rows))
from flask
import stream_with_context, request

@app.route('/stream')
def streamed_response():
   def generate():
   yield 'Hello '
yield request.args['name']
yield '!'
return app.response_class(stream_with_context(generate()))

Suggestion : 3

If we run the code above os.system('echo $HOME') in the Python IDLE, we only see the 0 because the stdout means a terminal. To see the command output we should redirect it to a file, and the read from it:, Because a subprocess is independent, it executes concurrently with the original process. That is, the process that created the subprocess can go on to work on other things while the subprocess carries out its own work behind the scenes.,Writing to a process can be done in a very similar way. If we want to send data to the process's stdin, we need to create the Popen object with stdin=subprocess.PIPE.,Notice that the message created in the write_to_stdin.py process was printed to stdout and then the return value (None, None) was printed. That's because no pipes were set up to stdout or stderr.

Popen
Popen(['/bin/sh', '-c', args[0], args[1], ...])

Suggestion : 4

Flask comes with a built-in developmental web server, i.e., you do not need to run an external web server (such as Apache) during development.,The url_for(view_function_endpoint) helper function, which returns the URL for the given view function (route handler), works in Jinja2 template as well.,The built-in developmental web server provided by Flask is not meant for use in production. I will show you how to run a Flask app under Apache web server later.,Take note that the app.run() must be removed from if __name__ == '__main__': block, so as not to launch the Flask built-in developmental web server.

.wsgi
sys.path.append('/path/to/eclipse/plugins/org.python.pydev_x.x.x.x/pysrc')
WSGIDaemonProcess myflaskapp user = flaskuser group = www - data threads = 5 python - path = /path/to / eclipse / plugins / org.python.pydev_x.x.x.x / pysrc
/etc/environment
PYTHONPATH = '/path/to/eclipse/plugins/org.python.pydev_x.x.x.x/pysrc'
True
app = Flask(__name__)
app.debug = True # Enable reloader and debugger
   ......

   if __name__ == '__main__':
   app.run()
app.run()
app = Flask(__name__)
   ......
   if __name__ == '__main__':
   app.run(debug = True) # Enable reloader and debugger
 * Running on http: //127.0.0.1:5000/ (Press CTRL+C to quit)
    *
    Restarting with stat *
    Debugger is active!
    *
    Debugger pin code: xxx - xxx - xxx
    ......