The answer to that is pretty simple:
x.read()
<- reads the whole file into memory, inefficient- setting response to a file: very inefficient as the protocol for that object is an iterator. So you will send the file line by line. If it's binary you will send it with random chunk sizes even.
- setting
response
to a string: bad idea. It's an iterator as mentioned before, so you are now sending each character in the string as a separate packet.
The correct solution is to wrap the file in the file wrapper provided by the WSGI server:
from werkzeug.wsgi import wrap_filereturn Response(wrap_file(environ, yourfile), direct_passthrough=True)
The direct_passthrough
flag is required so that the response object does not attempt to iterate over the file wrapper but leaves it untouched for the WSGI server.