Differences between revisions 2 and 5 (spanning 3 versions)
Revision 2 as of 2008-05-07 22:53:47
Size: 1474
Editor: GlennTarbox
Comment:
Revision 5 as of 2008-11-14 13:41:51
Size: 1756
Editor: anonymous
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from GlennTarbox/TwistedTalk/100 Twisted Deconstructed
Line 8: Line 9:
[[Navigation(slides)]] <<Navigation(slides)>>
Line 32: Line 33:
d = O.doComplicatedLengthyCalc(a,b,c) # returns deferred d = O.doComplicatedLengthyCalc(a, b, c) # returns deferred
Line 37: Line 38:
def gotResult2(result) def gotResult2(result, i2, s2, specialLabel)
Line 39: Line 40:
d.addCallback(fn2, ...)    print i2, s2, specialLabel # will print "2, string2, special"
d.addCallback(gotResult2, 2, "string2", specialLabel="special")
Line 42: Line 44:

This is a very important passage... but don't hurt your brain!!
Line 46: Line 50:
...
Line 48: Line 51:
[[Navigation(siblings)]] <<Navigation(siblings)>>

Concurrency

We got Both Kinds

  • I/O Bound
  • Compute Bound

Waiting For Answers

  1. Just Wait... wasn't doing nothin' anyway
  2. Spawn Process
  3. Spawn Thread
  4. Non-blocking calls and "a plan"

Non-Blocking Calls

  • Register Callback

   1 O.registerCallback(fn, *args, **kargs) # fn can be function or method
   2 
   3 def fn(result, *args, **kargs)
   4   print "important message: ", result
  • Use Deferreds

   1 d = O.doComplicatedLengthyCalc(a, b, c) # returns deferred
   2 def gotResult(result)
   3   print "important message: ", result
   4   return "a more important message"
   5 d.addCallback(gotResult)             # first callback
   6 def gotResult2(result, i2, s2, specialLabel)
   7    print "gotResult2 returns: ", result # will print "a more important message"
   8    print i2, s2, specialLabel               # will print "2, string2, special"
   9 d.addCallback(gotResult2, 2, "string2", specialLabel="special")
  10 d.addErrback(errfn,...)    # exception handling for deferreds

This is a very important passage... but don't hurt your brain!!

If you need to call a method that returns a deferred within your callback chain, just return that deferred, and the result of the secondary deferred's processing chain will become the result that gets passed to the next callback of the primary deferreds processing chain

DsageNg/TwistedTalk/100_Twisted_Deconstructed (last edited 2008-11-14 13:41:51 by anonymous)