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.

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):
t = app.jinja_env.get_template(template_name)
rv =
return rv

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

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 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(['/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 must be removed from if __name__ == '__main__': block, so as not to launch the Flask built-in developmental web server.

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

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