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