~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/howto/listings/deferred/deferred_ex4.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 we'll see what happens when you use 'addBoth'.
 
11
"""
 
12
 
 
13
class Counter(object):
 
14
    num = 0
 
15
 
 
16
 
 
17
def handleFailure(f):
 
18
    print "errback"
 
19
    print "we got an exception: %s" % (f.getTraceback(),)
 
20
    f.trap(RuntimeError)
 
21
    return "okay, continue on"
 
22
 
 
23
def handleResult(result):
 
24
    Counter.num += 1
 
25
    print "callback %s" % (Counter.num,)
 
26
    print "\tgot result: %s" % (result,)
 
27
    return "yay! handleResult was successful!"
 
28
 
 
29
def failAtHandlingResult(result):
 
30
    Counter.num += 1
 
31
    print "callback %s" % (Counter.num,)
 
32
    print "\tgot result: %s" % (result,)
 
33
    print "\tabout to raise exception"
 
34
    raise RuntimeError, "whoops! we encountered an error"
 
35
 
 
36
def doThisNoMatterWhat(arg):
 
37
    Counter.num += 1
 
38
    print "both %s" % (Counter.num,)
 
39
    print "\tgot argument %r" % (arg,)
 
40
    print "\tdoing something very important"
 
41
    # we pass the argument we received to the next phase here
 
42
    return arg   
 
43
 
 
44
 
 
45
 
 
46
def behindTheScenes(result):
 
47
    # equivalent to d.callback(result)
 
48
 
 
49
    if not isinstance(result, failure.Failure): # ---- callback 
 
50
        try:
 
51
            result = handleResult(result)
 
52
        except:
 
53
            result = failure.Failure()
 
54
    else:                                       # ---- errback
 
55
        pass
 
56
 
 
57
 
 
58
    if not isinstance(result, failure.Failure): # ---- callback
 
59
        try:
 
60
            result = failAtHandlingResult(result)
 
61
        except:
 
62
            result = failure.Failure()
 
63
    else:                                       # ---- errback
 
64
        pass
 
65
 
 
66
 
 
67
    # ---- this is equivalent to addBoth(doThisNoMatterWhat)
 
68
 
 
69
    if not isinstance(result, failure.Failure): 
 
70
        try:
 
71
            result = doThisNoMatterWhat(result)
 
72
        except:
 
73
            result = failure.Failure()
 
74
    else:
 
75
        try:
 
76
            result = doThisNoMatterWhat(result)
 
77
        except:
 
78
            result = failure.Failure()
 
79
        
 
80
 
 
81
    if not isinstance(result, failure.Failure): # ---- callback
 
82
        pass
 
83
    else:                                       # ---- errback
 
84
        try:
 
85
            result = handleFailure(result)
 
86
        except:
 
87
            result = failure.Failure()
 
88
 
 
89
 
 
90
def deferredExample():
 
91
    d = defer.Deferred()
 
92
    d.addCallback(handleResult)
 
93
    d.addCallback(failAtHandlingResult)
 
94
    d.addBoth(doThisNoMatterWhat)
 
95
    d.addErrback(handleFailure)
 
96
 
 
97
    d.callback("success")
 
98
 
 
99
 
 
100
if __name__ == '__main__':
 
101
    behindTheScenes("success")
 
102
    print "\n-------------------------------------------------\n"
 
103
    Counter.num = 0
 
104
    deferredExample()