[Flask] Why is sending a file so slow?

Can Hoşgör canhosgor at gmail.com
Wed Oct 7 09:09:31 CEST 2015

If you are sure the problem is not the db. I would try the following:

Streaming responses:


But IMHO, the downsides of storing large files in the DB will outweigh
its benefits in the long run.

You can store contents somewhere on the filesystem and just store the
file path in the db. You don't need to sacrifice history this way,
just make sure you put contents to new files instead of overwriting
old ones. (You can use file hash to generate unique filenames)

On Fri, Oct 2, 2015 at 4:33 PM, Malphas Wats <malphas at subdimension.co.uk> wrote:
> 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):
>     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
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask

More information about the Flask mailing list