Python Flask + nginx fcgi - output a great answer?

I am using Python Flask + nginx with FCGI.

For some queries, I have to print large answers. Typically, these responses are retrieved from the socket. I am currently doing this answer:

response = [] while True: recv = s.recv(1024) if not recv: break response.append(recv) s.close() response = ''.join(response) return flask.make_response(response, 200, { 'Content-type': 'binary/octet-stream', 'Content-length': len(response), 'Content-transfer-encoding': 'binary', }) 

The problem is that I really don't need data. I also have a way to determine the exact length of the response received from the socket. Therefore, I need a good way to send HTTP headers, and then start outputting directly from the socket, and not collect it in memory, and then deliver it to nginx (possibly by some thread).

I could not find a solution to this seemingly common problem. How will this be achieved?

Thanks!

+6
source share
1 answer

if the response in flask.make_response is iterable, it will be iterated to get the response, and each line will be written to the output stream on it.

which means that you can also return a generator that will give a result when iterates. if you know the length of the content, then you can (and should) pass it as a header.

simple example:

 from flask import Flask app = Flask(__name__) import sys import time import flask @app.route('/') def generated_response_example(): n = 20 def response_generator(): for i in range(n): print >>sys.stderr, i yield "%03d\n" % i time.sleep(.2) print >>sys.stderr, "returning generator..." gen = response_generator() # the call to flask.make_response is not really needed as it happens imlicitly # if you return a tuple. return flask.make_response(gen ,"200 OK", {'Content-length': 4*n}) if __name__ == '__main__': app.run() 

if you run this and try it in the browser, you will see a good score of confusion ...

(the content type is not set, because it seems like I'm doing what my browser waits until all the content has been transferred before the page is displayed. wget -qO - localhost:5000 does not have these problems.

+10
source

Source: https://habr.com/ru/post/916081/


All Articles