Thu, 31 Jan

Why PSGI uses an array reference instead of a string in a response body?

I tweeted this question and got an answer from the designer, @miyagawa.

In WSGI (PEP 333), an response body is an iterable.

When called by the server, the application object must return an iterable yielding zero or more strings. This can be accomplished in a variety of ways, such as by returning a list of strings, or by the application being a generator function that yields strings, or by the application being a class whose instances are iterable. Regardless of how it is accomplished, the application object must always return an iterable yielding zero or more strings.

And the spec recommends a single-element list containg a single string.

The corresponding approach in WSGI is for the application to simply return a single-element iterable (such as a list) containing the response body as a single string. This is the recommended approach for the vast majority of application functions, that render HTML pages whose text easily fits in memory.

mod_wsgi explains what happens when you return a string instead of a list.

The worst case of this is where instead of returning a list containing strings, a single string is returned. The problem with a string is that when it is iterated over, a single character of the string is yielded each time.

The Twitter Stack

Johan Oskarsson explained about their stack from Finagle to jvmgcprof.

My favorite stories around GitHub