Why are CherryPy static downloads so slow?

If I mount the static directory in CherryPy, like this:

wwwroot_config = { '/': { 'tools.staticdir.on': True, 'tools.staticdir.dir': '/path/to/dir' } } cherrypy.tree.mount(root, '/', config = wwwroot_config) 

Downloading files from this directory is rather slow.

However, if I create my own WSGI application ...

 self.wsgi_server = wsgiserver.CherryPyWSGIServer((self.bindaddress, self.port), self.download_file, numthreads = 1) 

With self.download_file containing basically:

 return serve_file(theFile, "application/x-download", "attachment", os.path.basename(theFile), debug = True) 

I get a speed that is 4-5 times faster.

However, this method is not so flexible because the headers that serve_file adds to the request (for example, range headers and content lengths) do not return in response - I have to do it myself.

Is there something I can do to make the first way faster?

+4
source share
1 answer

I had the same problem before, although on Windows computers, as I recall. Do you use CherryPy on Windows? There seems to be a bug in CherryPy 3.5.0 that returns the wrong Content-Length header for static files, which causes the browser to sit idle, expecting more bytes that don't exist.

I did not investigate the problem in detail, but you can check the Content-Length header sent by the server and see if it matches the actual size of the static files. A temporary solution may be to manually set (or remove) the Content-Length header in the "before_handler" hook.

+2
source

All Articles