Snakelets and WSGI

Robert Brewer fumanchu at amor.org
Tue Oct 12 15:33:32 EDT 2004


Alan Kennedy:
> 
> > Ah, one of the key questions! And the answer is that, iff you write 
> > Snakelets according to the WSGI API, then it should be 
> > easily usable on
> > asynchronous architectures. This is achieved through the 
> > use of python 
> > iterators (e.g. generators), so that application frameworks 
> > generate
> > content on demand, when the server/gateway is ready to 
> > transmit them.

Irmen de Jong:
> Interesting idea. I haven't thought about generating output this way.
> ...
> Sounds neat, but I think this is a hard thing to do for me.
> Anyway we'll see. First have to read the WSGI doc thoroughly.
> ...
> "using generators". I'm using them
> for other things, but not the generation of output...

Hoepfully you'll find it's simple. You should be able to take
YPage.write() for example, and queue the output into an iterable which
the WSGI server can then iterate over.

The tough part will be that anyone already using Snakelets will have to
rewrite their code if they want to *benefit* from the switch to
iteration; they'll have to change "write()" to "yield value". This
unfortunately means most CGI scripts will have to be generators. This
can be mitigated somewhat by allowing code in, say, overridden
Snakelets.serve() methods to return content as well as yield content (so
not every block of code which calls write() has to be turned into a
generator). Just examine the result of the callable in question; if it
doesn't have a next() attribute, wrap it in iter(list(result)). Kludgy
but it works.



Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list