Quantcast
Viewing latest article 20
Browse Latest Browse All 43

Answer by Armin Ronacher for Werkzeug response too slow

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.


Viewing latest article 20
Browse Latest Browse All 43

Trending Articles