I'm working at a web application in Python/Twisted.
I want the user to be able to download a very big file (> 100 Mb). I don't want to load all the file in memory (of the server), of course.
server side I have this idea:
...
request.setHeader('Content-Type', 'text/plain')
fp = open(fileName, 'rb')
try:
r = None
while r != '':
r = fp.read(1024)
request.write(r)
finally:
fp.close()
request.finish()
I expected this to work, but I have problems: I'm testing with FF... It seems the browser make me wait until the file is completed downloaded, and then I have the open/save dialog box.
I expected the dialog box immediately, and then the progress bar in action...
Maybe I have to add something in the Http header... Something like the size of the file?
Two big problems with the sample code you posted are that it is non-cooperative and it loads the entire file into memory before sending it.
Remember that Twisted uses cooperative multitasking to achieve any sort of concurrency. So the first problem with this snippet is that it is a while loop over the contents of an entire file (which you say is large). This means the entire file will be read into memory and written to the response before anything else can happen in the process. In this case, it happens that "anything" also includes pushing the bytes from the in-memory buffer onto the network, so your code will also hold the entire file in memory at once and only start to get rid of it when this loop completes.
So, as a general rule, you shouldn't write code for use in a Twisted-based application that uses a loop like this to do a big job. Instead, you need to do each small piece of the big job in a way that will cooperate with the event loop. For sending a file over the network, the best way to approach this is with producers and consumers. These are two related APIs for moving large amounts of data around using buffer-empty events to do it efficiently and without wasting unreasonable amounts of memory.
You can find some documentation of these APIs here:
http://twistedmatrix.com/projects/core/documentation/howto/producers.html
Fortunately, for this very common case, there is also a producer written already that you can use, rather than implementing your own:
http://twistedmatrix.com/documents/current/api/twisted.protocols.basic.FileSender.html
You probably want to use it sort of like this:
You can read more about
NOT_DONE_YET
and other related ideas the "Twisted Web in 60 Seconds" series on my blog, http://jcalderone.livejournal.com/50562.html (see the "asynchronous responses" entries in particular).If this really is
text/plain
content, you should seriously consider sending it withContent-Encoding: gzip
whenever a client indicates they can handle it. You ought to see huge bandwidth savings. Additionally, if this is a static file, what you really want to do is usesendfile(2)
. As for browsers not doing what you expect in terms of downloading things, you might want to look at theContent-Disposition
header. So anyhow, the logic goes like this:If the client indicates they can handle
gzip
encoding via theAccept-Encoding
header (e.g.Accept-Encoding: compress;q=0.5, gzip;q=1.0
orAccept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0
or similar) then compress the file, cache the compressed result somewhere, write the correct headers for the response (Content-Encoding: gzip
,Content-Length: n
,Content-Type: text/plain
, etc), and then usesendfile(2)
(however that may or may not have been made available in your environment) to copy the content from the open file descriptor into your response stream.If they don't accept
gzip
, do the same thing, but without gzipping first.Alternatively, if you have Apache, Lighttpd, or similar acting as a transparent proxy in front of your server, you could use the
X-Sendfile
header, which is exceedingly fast:Yes, the Content-Length header will give you the progress bar you desire!
Here is an example of downloading files in chunks using urllib2, which you could use from inside of a twisted function call