[Twisted-Python] Twisted-webserver hangs when serving big files

Moshe Zadka m at moshez.org
Sat Jun 7 09:08:56 EDT 2003

On Sat, 7 Jun 2003, "Thomas Weholt" <2002 at weholt.org> wrote:

> I'm trying to create a webserver using code from HEP Message Server, which
> in turn is based on Twisted. I cannot use rpys in this project due to
> limitations on other parts of the system. I need to be able to send huge
> files in a non-blocking manner, preferrably supporting Byte-ranges and
> partial downloads too, but that's secondary to just being able to download
> normally.
> My code hangs when the request is for big files.
> class RequestHandler(Request):

Why in god's name are you overriding the Request object?
Instead of using, say, a root resource which does simple dispatching?
That would allow you to actually *use* the code in Twisted.Web, instead
of hacking up your own stuff, which is buggy :)

>     def sendRequestedFile(self):
>         htmlFile = open(pagefilename, 'r+b')
>         self.setHeader("Content-type",mimetype)
>         chunk = htmlFile.read(1024)
>         while chunk:
>             self.write(chunk)
>             chunk = htmlFile.read(1024)
>         htmlFile.close()

Yes, when you write hanging code, it hangs. You are sending the whole file
in one go. Soon, .send() overflows the OS buffers, and then Twisted dutifully
starts to buffer for you. That should give the symptoms you see.

I suggest just substuting it with static.File(pagefilename).render(self)

Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/

More information about the Twisted-Python mailing list