Concurrency
We got Both Kinds
- I/O Bound
- Compute Bound
Waiting For Answers
- Just Wait... wasn't doing nothin' anyway
- Spawn Process
- Spawn Thread
- Non-blocking calls and "a plan"
Non-Blocking Calls
- Register Callback
- 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