Thx guys.<br><br>now quick question on the usage of thread local storage.<br><br>In my case I have the below object model:<br><br>class Base(object):'<br> def __init__(self):<br> self.__service = None <br> def _GetService():
<br> if not hasattr(threading.currentThread(), 'service'):<br> threading.currentThread().service = Service()<br> self.__service = threading.currentThread().service<br> <br> return self.__service
<br><br>class Child1(Base):'<br> def DoSomething():<br> service = self._GetService()<br> # use service<br><br>class Child2(Base):'<br>
def DoSomething():<br>
service = self._GetService()<br>
# use service<br>
<br>The above Child classes are used by a controller:<br><br>class Controller(object):<br> def process(self):<br> c1 = Child1() <br> c1.DoSomething()<br> ....<br> ...<br> c2 = Child2() <br>
c2.DoSomething()<br>
<br>Using the above technique the "service" is instantiated only one time i.e. as soon as I create the first instance of the Child class abd associated with the current thread for future instantiation of any Child class.
<br><br>Now in this scenario how can I use thread local ? Where do I keep the thread local object as in my case I am instantiating the Child classes ? Using currentThread() always gives me the same thread instance for a given request and I can bypass instantiating the Service class by simply returning the "service" attribute already attached to the current thread.
<br><br>Any suggestion appreciated!<br><br>- A<br><br><br><br><br><br><br><div><span class="gmail_quote">On 4/15/07, <b class="gmail_sendername">Kent Johnson</b> <<a href="mailto:kent37@tds.net">kent37@tds.net</a>> wrote:
</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Andreas Kostyrka wrote:<br>> * Kent Johnson <<a href="mailto:kent37@tds.net">kent37@tds.net
</a>> [070414 19:53]:<br>>> That's a good point. Does anyone know when to prefer threading.local()<br>>> vs thread attributes?<br>> It's design question, I guess:<br>><br>> *) if you have thread subclasses, then use thread attributes.
<br>> *) if you have standard threads, then use thread.local().<br>><br>> The idea is, that it's "rude" to stick attributes on an object that is<br>> not owned by you.<br>><br>> Rationale:<br>
> *) Somebody might decide to make threading.Thread be a new style<br>> object with __slots__ => your code breaks.<br>><br>> I know, it's unprobably, but if you derive a subclass, you can be at<br>> least sure that the object will have a __dict__ ;)
<br><br>If you use threading.local() you can be sure the names you use don't<br>conflict with any attributes of the thread.<br><br>Kent<br>_______________________________________________<br>Tutor maillist - <a href="mailto:Tutor@python.org">
Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br>