<div class="gmail_quote">On Thu, Jul 30, 2009 at 4:27 PM, Terry Jones <span dir="ltr">&lt;<a href="mailto:terry@jon.es">terry@jon.es</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 class="im">&gt; Right.  Everything you could do synchronously, you can do asynchronously<br>
&gt; (and a bit more too).  It just /looks/ really weird.<br>
<br>
</div>At some point I realized that it helps to realize that code that looks<br>
synchronous really isn&#39;t at all. Probably all the programming that almost<br>
all of us will ever do is not synchronous. You type<br>
<br>
  x = y + 3<br>
<br>
and you get into the habit of thinking that x just got assigned the value<br>
of y + 3. But there&#39;s a whole set of levels of operations, all invisible to<br>
you, that are frantically rearranging atoms behind the scenes to pull off<br>
this tiny miracle. Instead of trying to wrap your head around the so-called<br>
&quot;weird&quot; asynchronous operation of Twisted, it&#39;s easier to let fall the<br>
comforting illusion that you&#39;re in the synchronous world to begin with -<br>
whatever that&#39;s supposed to mean (it&#39;s more like a fairy tale IMO). You&#39;re<br>
not.<br>
<br>
It&#39;s more accurate to think that that line of code arranges for x to<br>
*eventually* hold the value of y + 3. Then it&#39;s a small step to thinking<br>
that<br>
<br>
  x = y + 3<br>
  z = f(x)<br>
<br>
is just another slightly bigger part of the same illusion. Those lines of<br>
code aren&#39;t executed one after another at all. You&#39;re just brought up with<br>
the fairy tale that they are - or at least you study computer science and<br>
spend so much time with high-level languages that you start to take it for<br>
granted that they are done one after the other. But the second line is<br>
*eventually* executed after the first, assuming nothing went wrong (e.g., y<br>
could be None).<br>
<br>
At that point - everything is asynchronous - you have the right mindset to<br>
think perfectly clearly about Twisted code. And there&#39;s no jump to make at<br>
all. The only things you have to get your head around are 1) the fact that<br>
Twisted code doesn&#39;t give you the comforting linear illusion of so-called<br>
synchronous code as you type it in your editor (inlineCallbacks helps a lot<br>
with improving that), and 2) that Twisted&#39;s deferreds are the way of<br>
maintaining the flow of results (both correct and incorrect) through time<br>
as your program executes. I.e., you&#39;ve lost the step-at-a-time line-by-line<br>
results computed on line N are available on line N+1 flow of code you can<br>
so easily read in your editor, and in exchange you have deferreds.<br>
<br>
I could go on, obviously :-) But I just wanted to point out that the shift<br>
from thinking &quot;synchronously&quot; to &quot;asynchronously&quot; is much easier to make if<br>
you realize the former is a convenient illusion and in fact you&#39;ve always<br>
been doing things asynchronously.<br>
<font color="#888888"><br>
Terry<br>
</font><div><div></div><div class="h5"></div></div></blockquote><div><br>Thanks for this, Terry.  I&#39;d never thought of it that way, and it&#39;s quite a good point.<br><br>Kevin Horn <br></div></div><br>