[Web-SIG] yield considered harmful (was: x-wsgiorg.flush)

René Dudfield renesd at gmail.com
Sat Oct 6 05:07:08 CEST 2007


I think 'streaming' is good for speeding up web pages when processing
takes a while.

I'll explain why...

Say your page takes 0.2 seconds to process.

If you wait until 0.2 seconds is up, then the first bytes that will
come to the browser will arrive in at least 0.2 seconds.  Whereas if
you send data as soon as its ready, then the user will be able to see
some of that data more quickly - and possibly make more requests
sooner.

However if your application can not send data until it is all ready
anyway - which is the way with most python templating languages - then
you might as well send it all in one go.  Sending it all in one go is
faster, unless you can send data as a stream.

Sending the header of a html page right away is often very quick for
dynamic pages.  Since often that part is static - and it contains
links to other files - like css, js, and image files.  So yielding the
header part, then doing your database connection, and page
construction which takes longer will almost always be faster for the
user - than waiting for the entire page to be ready.


On 10/6/07, Phillip J. Eby <pje at telecommunity.com> wrote:
> At 11:57 AM 10/5/2007 -0300, Rob De Almeida wrote:
> >Phillip J. Eby wrote:
> >>I mean that you can't write a WSGI 2.0 application using a single
> >>generator function, because it has to return a tuple, not an
> >>iterator.  This will discourage people from thinking "yield" is a
> >>good way to build up their output, instead of using a StringIO or
> >>''.join() on a list of strings.
> >
> >Could you explain why using 'yield' is not recommended? Just
> >curious, because I use it all the time.
>
> Because you're slowing down your application's throughput.  The only
> reasons to yield multiple strings is when you are either:
>
> 1. Sending a file that's larger than you want to load into memory, or
>
> 2. You're doing "server push" and need to do some processing between payloads.
>
> _______________________________________________
> Web-SIG mailing list
> Web-SIG at python.org
> Web SIG: http://www.python.org/sigs/web-sig
> Unsubscribe: http://mail.python.org/mailman/options/web-sig/renesd%40gmail.com
>


More information about the Web-SIG mailing list