Related to discussions on the notebook and extending the core Sage
infrastructure, I stumbled across a thread on the twisted mailing list

http://twistedmatrix.com/pipermail/twisted-python/2009-July/020069.html

which discusses a new introduction to asynchronous programming, why its good
/ necessary / important and not all that difficult.

The report url is:

http://mumak.net/stuff/twisted-intro.html

Also, there's a small example on StackOverflow by Glyph which does a pretty
good job at de-mystifying some of the Asynchronous mystique.

http://stackoverflow.com/questions/80617/asychronous-programming-in-python-twisted/81456#81456

There's also an "in the works" part of the twisted document which attempts
to clarify what was a simply awful part of the previous twisted
documentation

http://ezyang.com/twisted/defer2.html

In the thread itself is a comment by Terry Jones which I include here

> Right.  Everything you could do synchronously, you can do asynchronously
> (and a bit more too).  It just /looks/ really weird.

At some point I realized that it helps to realize that code that looks
synchronous really isn't at all. Probably all the programming that almost
all of us will ever do is not synchronous. You type

 x = y + 3

and you get into the habit of thinking that x just got assigned the value
of y + 3. But there's a whole set of levels of operations, all invisible to
you, that are frantically rearranging atoms behind the scenes to pull off
this tiny miracle. Instead of trying to wrap your head around the so-called
"weird" asynchronous operation of Twisted, it's easier to let fall the
comforting illusion that you're in the synchronous world to begin with -
whatever that's supposed to mean (it's more like a fairy tale IMO). You're
not.

It's more accurate to think that that line of code arranges for x to
*eventually* hold the value of y + 3. Then it's a small step to thinking
that

 x = y + 3
 z = f(x)

is just another slightly bigger part of the same illusion. Those lines of
code aren't executed one after another at all. You're just brought up with
the fairy tale that they are - or at least you study computer science and
spend so much time with high-level languages that you start to take it for
granted that they are done one after the other. But the second line is
*eventually* executed after the first, assuming nothing went wrong (e.g., y
could be None).

At that point - everything is asynchronous - you have the right mindset to
think perfectly clearly about Twisted code. And there's no jump to make at
all. The only things you have to get your head around are 1) the fact that
Twisted code doesn't give you the comforting linear illusion of so-called
synchronous code as you type it in your editor (inlineCallbacks helps a lot
with improving that), and 2) that Twisted's deferreds are the way of
maintaining the flow of results (both correct and incorrect) through time
as your program executes. I.e., you've lost the step-at-a-time line-by-line
results computed on line N are available on line N+1 flow of code you can
so easily read in your editor, and in exchange you have deferreds.

I could go on, obviously :-) But I just wanted to point out that the shift
from thinking "synchronously" to "asynchronously" is much easier to make if
you realize the former is a convenient illusion and in fact you've always
been doing things asynchronously.

Terry

-- 
Glenn H. Tarbox, PhD ||  206-274-6919
http://www.tarbox.org

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to