[Web-SIG] Alternative to threading.local, based on the stack

Benji York benji at benjiyork.com
Fri Jul 4 16:13:31 CEST 2008


On Fri, Jul 4, 2008 at 9:23 AM, Iwan Vosloo <iwan at reahl.org> wrote:
> On Fri, 2008-07-04 at 13:39 +0100, Matt Goodall wrote:
>> The ideal solution is, of course, to pass everything around to whatever
>> needs it. However, there's really tedious at times.
>>
>> Whatever the architecture of the web server there is always a request
>> or, in case of WSGI, an env dict. Therefore, request-scope objects
>> should be associated with the request.
>
> True, but even passing a request or env dict around to everyone gets
> tedious don't you think?

It can.  Zope 3 makes a pretty good compromise here.  The "top level"
object involved in handing the request -- a view -- gets the request
object explicitly passed as a parameter.  If the view wants to pass the
request to function calls or other objects, then it's free to do so.

But, if at some point you find yourself without a reference to the
current request and really need it, you can get it "out of thin air" by
calling (essentially) get_request().

The Zope 3 publisher precesses requests using a thread pool, so
get_request() is implemented by stashing the request object in the
tread-local storage prior to processing the request and digging it back
out if requested.

Other implementations could store the request somewhere else, but the
idea is the same.
-- 
Benji York


More information about the Web-SIG mailing list