At 03:43 PM 3/30/2009 +0300, Sergey Schetinin wrote:
Did you consider a variation that eliminates the start_response but
returns status and headers as first item of the iterable? Considering
how response is usually generated it could save some code in some
cases and wouldn't add any overhead in others, it also has a pleasant
similarity to the HTTP response format:

  def simple_app(environ):
      """Simplest possible application object"""
      status = '200 OK'
      response_headers = [('Content-type','text/plain')]
      yield status, response_headers
      yield 'Hello world!\n'


I thought about this, and also a variant where status is in the headers, as a 'Status' header ala CGI, with a default of '200 OK'. In which case, the following would both be valid apps:

    def simple_app(environ):
        return [('Content-type','text/plain')], "Hello world!\n"

    def simple_app(environ):
        yield [('Content-type','text/plain')]
        yield "Hello world!\n"

However, the main reason I suggested the fixed 3-tuple return is to *discourage* the use of generators as response bodies, because a lot of people think that they should use yield to build up the output, ala 'print' in CGI. This is really bad for performance, given that iterator yielding is supposed to be used to flush the output channel.

Unless you're transmitting a large file, you really should just be returning one string with the entire body in the common case. As you point out, it's not that difficult to wrap something with a decorator to use one of these simple styles if you need to.
_______________________________________________
Web-SIG mailing list
Web-SIG@python.org
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: 
http://mail.python.org/mailman/options/web-sig/archive%40mail-archive.com

Reply via email to