[Flask] Why is sending a file so slow?
malphas at subdimension.co.uk
Fri Oct 2 15:33:58 CEST 2015
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):
def download_resource(resource_id, filename):
### HACK ALERT! WHOOP WHOOP WHOOP ###
if 'If-Modified-Since' in request.headers:
resource = model.lesson.get_lesson_resource(resource_id)
r = Response(resource['file_data'], mimetype=resource['mime_type'])
%d %b %Y %H:%M:%S GMT"))
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 <
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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Flask