Thanks for the help on this everyone <br><br><br clear="all">Tim Hughes<br>mailto:<a href="mailto:thughes@thegoldfish.org">thughes@thegoldfish.org</a><br><br>Without religion, people wouldn&#39;t pray for peace, they would work towards it.<br>

<br><br><div class="gmail_quote">On Fri, Apr 24, 2009 at 8:33 PM, Drew Smathers <span dir="ltr">&lt;<a href="mailto:drew.smathers@gmail.com">drew.smathers@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="h5">On Fri, Apr 24, 2009 at 2:28 AM, Nathan &lt;<a href="mailto:nathan.stocks@gmail.com">nathan.stocks@gmail.com</a>&gt; wrote:<br>
&gt; On Thu, Apr 23, 2009 at 9:34 PM, Tim Allen &lt;<a href="mailto:tim@commsecure.com.au">tim@commsecure.com.au</a>&gt; wrote:<br>
&gt;&gt; Tim Hughes &lt;<a href="mailto:thughes@thegoldfish.org">thughes@thegoldfish.org</a>&gt; wrote:<br>
&gt;&gt;&gt; Am I completly missing the point here or is there something incorrect with<br>
&gt;&gt;&gt; my code.<br>
&gt;&gt;<br>
&gt;&gt; Yes, it seems you have missed the point somewhere.<br>
&gt;&gt;<br>
&gt;&gt;&gt;     def blocking_method(self, duration=5):<br>
&gt;&gt;&gt;         &quot;&quot;&quot;block the instance for a specified duration&quot;&quot;&quot;<br>
&gt;&gt;&gt;         started = time.asctime()<br>
&gt;&gt;&gt;         time.sleep(duration)<br>
&gt;&gt;&gt;         data = &quot;I started at %s and i slept %d seconds then woke at %s&quot; %<br>
&gt;&gt;&gt; (started, duration, time.asctime())<br>
&gt;&gt;&gt;         return data<br>
&gt;&gt;<br>
&gt;&gt;&gt;     def blocking_method_fixed(self, duration=5):<br>
&gt;&gt;&gt;         d = Deferred()<br>
&gt;&gt;&gt;         d.callback(self.blocking_method(duration))<br>
&gt;&gt;&gt;         return d<br>
&gt;&gt;<br>
&gt;&gt; Your blocking_method_fixed() wraps the result of blocking_method() in a<br>
&gt;&gt; Deferred, but it doesn&#39;t actually do anything to prevent<br>
&gt;&gt; blocking_method() from blocking. Deferreds have no magical ability to<br>
&gt;&gt; prevent things from blocking on their own, they are just a tool you can<br>
&gt;&gt; use to handle some of the control-flow issues that arise when writing<br>
&gt;&gt; non-blocking code.<br>
&gt;&gt;<br>
&gt;&gt; For example, here&#39;s a method that does more or less what<br>
&gt;&gt; blocking_method() does, but in a non-blocking manner:<br>
&gt;&gt;<br>
&gt;&gt;    def non_blocking_method(self, duration=5):<br>
&gt;&gt;        started = time.asctime()<br>
&gt;&gt;        d = Deferred()<br>
&gt;&gt;        reactor.callLater(duration, d.callback, None)<br>
&gt;&gt;<br>
&gt;&gt;        def do_stuff_after_timeout(result):<br>
&gt;&gt;            # Here, &#39;result&#39; will contain the None we passed to<br>
&gt;&gt;            # callLater<br>
&gt;&gt;            data = &quot;I started at %s and I slept %d seconds then woke &quot; \<br>
&gt;&gt;                   &quot;at %s&quot; % (started, duration, time.asctime())<br>
&gt;&gt;            return data<br>
&gt;&gt;        d.addCallback(do_stuff_after_timeout)<br>
&gt;&gt;        return d<br>
&gt;&gt;<br>
&gt;&gt; See how the method does some initial preparation, then schedules a<br>
&gt;&gt; callback to be run after the long-running operation has completed.<br>
&gt;&gt;<br>
&gt;&gt; If you have a long-running network call to do instead of a simple<br>
&gt;&gt; sleep, there&#39;s probably a Twisted API or addon that will perform the<br>
&gt;&gt; call and give you back a Deferred, rather than blocking until it has an<br>
&gt;&gt; answer. If you&#39;re trying to use a third-party client library that<br>
&gt;&gt; doesn&#39;t offer a non-blocking API, about the only thing you can do is<br>
&gt;&gt; call that API in a thread, using the reactor.callInThread() method (but<br>
&gt;&gt; note that Twisted is not thread-safe, so the thing you call in a thread<br>
&gt;&gt; can&#39;t use any of Twisted&#39;s functionality except via<br>
&gt;&gt; reactor.callFromThread())<br>
&gt;<br>
&gt; The above email makes a great FAQ item, IMO.  I know there&#39;s already<br>
&gt; docs covering this, but this issue comes up soooo many times (it took<br>
&gt; me a month to wrap my head around it initially) that it wouldn&#39;t hurt<br>
&gt; to hit the issue from many directions in many styles.<br>
&gt;<br>
<br>
</div></div>It is one of the FAQ items:<br>
<br>
<a href="http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoIuseDeferredstomakemyblockingcodenon-blocking" target="_blank">http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoIuseDeferredstomakemyblockingcodenon-blocking</a><br>

<br>
Apparently there&#39;s also been some stitchwork done to commemorate this.<br>
<font color="#888888"><br>
-Drew<br>
</font><div><div></div><div class="h5"><br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</div></div></blockquote></div><br>