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

Matt Goodall matt at pollenation.net
Mon Jul 7 14:48:03 CEST 2008


Iwan Vosloo wrote:
> On Fri, 2008-07-04 at 13:39 +0100, Matt Goodall wrote:
>> Iwan Vosloo wrote:
>> You're correct that Twisted Web does not allocate a thread per request.
>> All requests are handled by an event loop in the main thread.
> 
>> In Twisted, the call stack tends to gets fragmented during a sequence of
>> asynchronous calls because of its callback mechanism. Basically, you're
>> hopping in and out of the Twisted reactor (the event mainloop) all the
>> time. Leaving something in the call stack would not work at all.
> 
> Couldn't you put something in the call stack each time in the main loop,
> before calling a callback (which will be popped again when that callback
> returns to the main loop)?

Yes, that's probably achievable by subclassing Deferred (the callback
class) and using a closure to reinstate the context before the callback
function is called. Perhaps I'll give it a go out of interest.

However, I'm not convinced it's a good idea and I suspect the Twisted
developers would sooner pluck out their eyeballs (or worse still, mine!)
than allow it into Twisted core ;-).

> 
>> 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?

Yes, it can be tedious but I believe explicit arg passing is necessary
to make code readable, testable and reusable.

If it's web-related code then give it the request, it will almost
certainly need it. Otherwise, don't.

I would even advocate extracting request-scope objects, e.g. a database
connection, the current user, etc, as early as possible and passing them
around explicitly (along with the request, if necessary).

I've made the mistake of relying on magic contexts in the past. I'm
still trying to fix things.


- Matt


-- 
Matt Goodall
Technical Director, Pollenation Internet Ltd

Registered Number: 4382123
Registered Office: 237 Lidgett Lane, Leeds, West Yorkshire, LS17 6QR
A member of the Brunswick MCL Group of Companies

w: http://www.pollenation.net/
e: matt at pollenation.net
t: +44 (0) 113 2252500

This message may be confidential and the views expressed may not reflect
the views of my employers. Please read http://eudaimon-group.com/email
if you are uncertain what this means.


More information about the Web-SIG mailing list