django-sendfile icon indicating copy to clipboard operation
django-sendfile copied to clipboard

Backend using environ['wsgi.file_wrapper']

Open edevil opened this issue 13 years ago • 4 comments

The WSGI protocol specifies an optional mechanism for higher performance file transmission.

There is at least one wsgi server that implements this: uWSGI.

A backend that takes advantage of this mechanism would be very interesting. Is it possible to write one using the current API?

edevil avatar Jun 08 '12 10:06 edevil

Hmmm. I'm not quite sure. The app only returns a Django response object, so not sure how you'd pass the FileWrapper to WSGI.

Some testing would be needed.

You can easily write your own backend. Just create a module with a single method:

def sendfile(request, filename, **kwargs)

Then set SENDFILE_BACKEND to the dotted name of your module (e.g. "mypackage.mymodule").

It might be as simple as something like:

def sendfile(request, filename, **kwargs):
return environ['wsgi.file_wrapper'](open(filename), block_size)

Just not sure whether Django will get confused. Might be you have to patch in extra things to the file wrapper object to keep Django happy.

Probably best to give it a go and see.

On Fri, 2012-06-08 at 03:19 -0700, André Cruz wrote:

The WSGI protocol specifies an optional mechanism for higher performance file transmission.

There is at least one wsgi server that implements this: uWSGI.

A backend that takes advantage of this mechanism would be very interesting. Is it possible to write one using the current API?


Reply to this email directly or view it on GitHub: https://github.com/johnsensible/django-sendfile/issues/6

johnsensible avatar Jun 08 '12 11:06 johnsensible

@johnsensible the sendfile method doesn't have access to the environ dict, and nor does django support returning anything other that django HttpResponse(s)

graingert avatar Apr 03 '14 16:04 graingert

gunicorn also provides this extensions, and the default django server will block gevent threads when it serves the file

graingert avatar Apr 03 '14 16:04 graingert

Dredging this up from years ago to document its possibility now. As of Django 1.8 (LTS), there is a new HttpResponse class called FileResponse (see ticket 24072) which does allow for a Django Response to populate the wsgi.file_wrapper (see commits 3d2cae0896ee8026d1c2c5d31e4c4c8f74f2fef4 and a9aec1154e5b65fcaf608801905a1bbafcfbfbf7)

I believe the implementation would probably simply be:

def sendfile(request, filename, **kwargs):
    return FileResponse(open(filename))

block size is a class attribute on FileResponse so doesn't need to be passed along (indeed I'm not sure it can be) Django will handle closing the object when the response finishes, if appropriate.

kezabelle avatar Jan 19 '17 08:01 kezabelle