[Tutor] threading.currentThread() always same across request ?
andreas at kostyrka.org
Sat Apr 14 19:34:13 CEST 2007
* Kent Johnson <kent37 at tds.net> [070414 19:30]:
> Arun Kumar PG wrote:
> > Guys,
> > I have a web application and I want to store an object per request so
> > thta that is available across all classes till end of request.
> > I am planning to write the below code in my entry program which is
> > executed as soon as a request comes:
> > entry.py
> > import threading
> > th = threading.currentThread()
> > th.service = somemod.Service()
> > then in other parts of program whereever I want to use service:
> > th = threading.currentThread ()
> > if hasattr(th, 'service'):
> > th.service.call_whatever()
> Rather than store your attributes directly in the thread, it would
> probably be better to use a threading.local() object as the container.
> I don't really know if it makes any difference but this is the supported
> mechanism for thread-local storage.
Well, the other mechanism is also supported, because subclassing
threads is allowed, and subclasses need to keep their attributes, even
if accessed via threading.currentThread().
But as Kent has stated, stick with the Session mechanism of your
> If you are using an existing web framework it probably has some kind of
> support for sessions. Using session objects might be another way to do this.
More information about the Tutor