[Flask] Why is sending a file so slow?

Malphas Wats malphas at subdimension.co.uk
Fri Oct 2 15:33:58 CEST 2015


Hi,

  I have an application that allows users to upload files. I agonized over
where to actually store these files, but in the end, since I wanted to keep
some form of file history, and make things easy on myself for backups and
things, I went with keeping everything in the Database.

I have a route that looks something like this (alright, exactly like this):

mod.route('/resource/<int:resource_id>/<filename>', methods=['GET'])
def download_resource(resource_id, filename):
    ### HACK ALERT! WHOOP WHOOP WHOOP ###
    if 'If-Modified-Since' in request.headers:
        return Response(status=304)
    resource = model.lesson.get_lesson_resource(resource_id)

    if resource:
        r = Response(resource['file_data'], mimetype=resource['mime_type'])
        r.headers.add('Last-Modified', resource['timestamp'].strftime("%a,
%d %b %Y %H:%M:%S GMT"))
        return r
    else:
        abort(404)

it's a little bit hacky, but overall it does what I want.
model.get_lesson_resource(resource_id) returns the blob from the database,
I then build a Response object from that and return it.

It is however REALLY REALLY slow at downloading the file - I can put a big
file in the application's 'static' directory and Flask can send it down at
my full network bandwith.

If I attempt to download a large file from the route above it slows to <
2k/s.

I don't understand why it's slow - it's not the database, because the blob
has been pulled out from there before the Response can be returned. I've
clearly done something heinous here but I can't see what it is - is there a
better way to send the file back to the client?

Thanks very much
-Mike
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20151002/6f436412/attachment.html>


More information about the Flask mailing list