[Twisted-web] Re: static file performance

David Bolen db3l.net at gmail.com
Tue Mar 4 19:13:03 EST 2008


Phil Christensen <phil at bubblehouse.org> writes:

> Hi All,
>
> The previous thread about http server performance made me think of
> something I've been meaning to ask about for a while.
>
> What is the best way to send static files to the client in
> twisted.web? I know web2 had some preliminary support for mmap for
> small files, but I know it didn't work the last time I checked, and
> I'm trying to stick with twisted.web for now, anyways.
>
> I've just been using Apache to serve static content, but I'd love to
> be able to use a pure-twisted solution.

I just use twisted.web.static.File - works fine.  You can subclass if
you want to give it a directory to provide access to all of its files,
but without enabling a directory listing (just override
directoryListing to return an error).

For single files, just instantiate static.File for the appropriate
file (on the fly or at startup), setting the content type if the
default mapping doesn't work.  For example, here's how I link in a
favorite icon file in one of my apps:

   favicon = static.File(os.path.join(static_dir, 'favicon.ico'),
                         defaultType='image/vnd.microsoft.icon')
   self.root.putChild('favicon.ico', favicon)

For large files, they're streamed appropriately (you can use the same
approach for your own dynamic content with static.FileTransfer, which I've
done for dynamic zip or mov file generation).

Generally, I structure my twisted.web applications so that on startup,
they construct an appropriate set of static.File objects and link them
into my root objects to provide full access to any static content.
Thus everything works in development with just the Twisted code.
Depending on deployment, you can leave it all that way, or if you
want, capture some of the static trees (like images) in a front end
server, so the requests never get to the Twisted app.  I often do
this, but it's probably more habit than anything else.

To be honest, while I'm sure there's a measurable overhead to serving
static content through Python/Twisted rather than a front end server
like Apache, I've never seen it be an issue.  Certainly streaming
large files are going to be more I/O bound than CPU bound anyway.

-- David




More information about the Twisted-web mailing list