3
# Copyright (c) 2009 Twisted Matrix Laboratories.
4
# See LICENSE for details.
6
from twisted.internet import defer
7
from twisted.python import failure, util
10
Here we have a slightly more involved case. The deferred is called back with a
11
result. the first callback returns a value, the second callback, however
12
raises an exception, which is handled by the errback.
16
class Counter(object):
21
print "we got an exception: %s" % (f.getTraceback(),)
24
def handleResult(result):
26
print "callback %s" % (Counter.num,)
27
print "\tgot result: %s" % (result,)
28
return "yay! handleResult was successful!"
30
def failAtHandlingResult(result):
32
print "callback %s" % (Counter.num,)
33
print "\tgot result: %s" % (result,)
34
print "\tabout to raise exception"
35
raise RuntimeError, "whoops! we encountered an error"
38
def behindTheScenes(result):
39
if not isinstance(result, failure.Failure): # ---- callback
41
result = handleResult(result)
43
result = failure.Failure()
48
if not isinstance(result, failure.Failure): # ---- callback
50
result = failAtHandlingResult(result)
52
result = failure.Failure()
57
if not isinstance(result, failure.Failure): # ---- callback
61
result = handleFailure(result)
63
result = failure.Failure()
66
def deferredExample():
68
d.addCallback(handleResult)
69
d.addCallback(failAtHandlingResult)
70
d.addErrback(handleFailure)
75
if __name__ == '__main__':
76
behindTheScenes("success")
77
print "\n-------------------------------------------------\n"