~vishvananda/nova/network-refactor

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/howto/listings/deferred/deferred_ex5.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Copyright (c) 2009 Twisted Matrix Laboratories.
 
4
# See LICENSE for details.
 
5
 
 
6
from twisted.internet import defer
 
7
from twisted.python import failure, util
 
8
 
 
9
"""
 
10
Now comes the more nuanced addCallbacks, which allows us to make a
 
11
yes/no (branching) decision based on whether the result at a given point is
 
12
a failure or not.
 
13
"""
 
14
 
 
15
class Counter(object):
 
16
    num = 0
 
17
 
 
18
 
 
19
def handleFailure(f):
 
20
    print "errback"
 
21
    print "we got an exception: %s" % (f.getTraceback(),)
 
22
    f.trap(RuntimeError)
 
23
    return "okay, continue on"
 
24
 
 
25
def handleResult(result):
 
26
    Counter.num += 1
 
27
    print "callback %s" % (Counter.num,)
 
28
    print "\tgot result: %s" % (result,)
 
29
    return "yay! handleResult was successful!"
 
30
 
 
31
def failAtHandlingResult(result):
 
32
    Counter.num += 1
 
33
    print "callback %s" % (Counter.num,)
 
34
    print "\tgot result: %s" % (result,)
 
35
    print "\tabout to raise exception"
 
36
    raise RuntimeError, "whoops! we encountered an error"
 
37
 
 
38
def yesDecision(result):
 
39
    Counter.num += 1
 
40
    print "yes decision %s" % (Counter.num,)
 
41
    print "\twasn't a failure, so we can plow ahead"
 
42
    return "go ahead!"
 
43
 
 
44
def noDecision(result):
 
45
    Counter.num += 1
 
46
    result.trap(RuntimeError)
 
47
    print "no decision %s" % (Counter.num,)
 
48
    print "\t*doh*! a failure! quick! damage control!"
 
49
    return "damage control successful!"
 
50
    
 
51
    
 
52
 
 
53
def behindTheScenes(result):
 
54
 
 
55
    if not isinstance(result, failure.Failure): # ---- callback
 
56
        try:
 
57
            result = failAtHandlingResult(result)
 
58
        except:
 
59
            result = failure.Failure()
 
60
    else:                                       # ---- errback
 
61
        pass
 
62
    
 
63
 
 
64
    # this is equivalent to addCallbacks(yesDecision, noDecision)
 
65
 
 
66
    if not isinstance(result, failure.Failure): # ---- callback 
 
67
        try:
 
68
            result = yesDecision(result)
 
69
        except:
 
70
            result = failure.Failure()
 
71
    else:                                       # ---- errback
 
72
        try:
 
73
            result = noDecision(result)
 
74
        except:
 
75
            result = failure.Failure()
 
76
 
 
77
    
 
78
    if not isinstance(result, failure.Failure): # ---- callback
 
79
        try:
 
80
            result = handleResult(result)
 
81
        except:
 
82
            result = failure.Failure()
 
83
    else:                                       # ---- errback
 
84
        pass
 
85
 
 
86
 
 
87
    # this is equivalent to addCallbacks(yesDecision, noDecision)
 
88
 
 
89
    if not isinstance(result, failure.Failure): # ---- callback 
 
90
        try:
 
91
            result = yesDecision(result)
 
92
        except:
 
93
            result = failure.Failure()
 
94
    else:                                       # ---- errback
 
95
        try:
 
96
            result = noDecision(result)
 
97
        except:
 
98
            result = failure.Failure()
 
99
 
 
100
 
 
101
    if not isinstance(result, failure.Failure): # ---- callback
 
102
        try:
 
103
            result = handleResult(result)
 
104
        except:
 
105
            result = failure.Failure()
 
106
    else:                                       # ---- errback
 
107
        pass
 
108
 
 
109
 
 
110
    if not isinstance(result, failure.Failure): # ---- callback
 
111
        pass
 
112
    else:                                       # ---- errback
 
113
        try:
 
114
            result = handleFailure(result)
 
115
        except:
 
116
            result = failure.Failure()
 
117
 
 
118
 
 
119
def deferredExample():
 
120
    d = defer.Deferred()
 
121
    d.addCallback(failAtHandlingResult)
 
122
    d.addCallbacks(yesDecision, noDecision) # noDecision will be called
 
123
    d.addCallback(handleResult) # - A -
 
124
    d.addCallbacks(yesDecision, noDecision) # yesDecision will be called
 
125
    d.addCallback(handleResult)  
 
126
    d.addErrback(handleFailure)
 
127
 
 
128
    d.callback("success")
 
129
 
 
130
 
 
131
if __name__ == '__main__':
 
132
    behindTheScenes("success")
 
133
    print "\n-------------------------------------------------\n"
 
134
    Counter.num = 0
 
135
    deferredExample()
 
136