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):&#39;<br>&nbsp; def __init__(self):<br>&nbsp; &nbsp; self.__service = None <br>&nbsp; def _GetService():
<br>&nbsp;&nbsp;&nbsp; if not hasattr(threading.currentThread(), &#39;service&#39;):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; threading.currentThread().service = Service()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.__service =&nbsp; threading.currentThread().service<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; return self.__service
<br><br>class Child1(Base):&#39;<br>&nbsp; def DoSomething():<br>&nbsp;&nbsp;&nbsp; service = self._GetService()<br>&nbsp;&nbsp;&nbsp; # use service<br><br>class Child2(Base):&#39;<br>
&nbsp; def DoSomething():<br>
&nbsp;&nbsp;&nbsp; service = self._GetService()<br>
&nbsp;&nbsp;&nbsp; # use service<br>
<br>The above Child classes are used by a controller:<br><br>class Controller(object):<br>&nbsp; def process(self):<br>&nbsp; &nbsp; c1 = Child1()&nbsp; <br>&nbsp;&nbsp;&nbsp; c1.DoSomething()<br>&nbsp;&nbsp;&nbsp; ....<br>&nbsp;&nbsp;&nbsp; ...<br>&nbsp; &nbsp; c2 = Child2()&nbsp; <br>
&nbsp;&nbsp;&nbsp; c2.DoSomething()<br>
<br>Using the above technique the &quot;service&quot; 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 &quot;service&quot; 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> &lt;<a href="mailto:kent37@tds.net">kent37@tds.net</a>&gt; 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>&gt; * Kent Johnson &lt;<a href="mailto:kent37@tds.net">kent37@tds.net
</a>&gt; [070414 19:53]:<br>&gt;&gt; That&#39;s a good point. Does anyone know when to prefer threading.local()<br>&gt;&gt; vs thread attributes?<br>&gt; It&#39;s design question, I guess:<br>&gt;<br>&gt; *) if you have thread subclasses, then use thread attributes.
<br>&gt; *) if you have standard threads, then use thread.local().<br>&gt;<br>&gt; The idea is, that it&#39;s &quot;rude&quot; to stick attributes on an object that is<br>&gt; not owned by you.<br>&gt;<br>&gt; Rationale:<br>
&gt; *) Somebody might decide to make threading.Thread be a new style<br>&gt; object with __slots__ =&gt; your code breaks.<br>&gt;<br>&gt; I know, it&#39;s unprobably, but if you derive a subclass, you can be at<br>&gt; 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&#39;t<br>conflict with any attributes of the thread.<br><br>Kent<br>_______________________________________________<br>Tutor maillist&nbsp;&nbsp;-&nbsp;&nbsp;<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>