~soren/nova/iptables-security-groups

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/howto/listings/deferred/deferred_ex2.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
This example shows an important concept that many deferred newbies
 
11
(myself included) have trouble understanding. 
 
12
 
 
13
when an error occurs in a callback, the first errback after the error
 
14
occurs will be the next method called. (in the next example we'll
 
15
see what happens in the 'chain' after an errback).
 
16
"""
 
17
 
 
18
class Counter(object):
 
19
    num = 0
 
20
 
 
21
def handleFailure(f):
 
22
    print "errback"
 
23
    print "we got an exception: %s" % (f.getTraceback(),)
 
24
    f.trap(RuntimeError)
 
25
 
 
26
def handleResult(result):
 
27
    Counter.num += 1
 
28
    print "callback %s" % (Counter.num,)
 
29
    print "\tgot result: %s" % (result,)
 
30
    return "yay! handleResult was successful!"
 
31
 
 
32
def failAtHandlingResult(result):
 
33
    Counter.num += 1
 
34
    print "callback %s" % (Counter.num,)
 
35
    print "\tgot result: %s" % (result,)
 
36
    print "\tabout to raise exception"
 
37
    raise RuntimeError, "whoops! we encountered an error"
 
38
 
 
39
 
 
40
 
 
41
def behindTheScenes(result):
 
42
    # equivalent to d.callback(result)
 
43
 
 
44
    # now, let's make the error happen in the first callback
 
45
    
 
46
    if not isinstance(result, failure.Failure): # ---- callback
 
47
        try:
 
48
            result = failAtHandlingResult(result)
 
49
        except:
 
50
            result = failure.Failure()
 
51
    else:                                       # ---- errback
 
52
        pass
 
53
 
 
54
 
 
55
    # note: this callback will be skipped because
 
56
    # result is a failure
 
57
 
 
58
    if not isinstance(result, failure.Failure): # ---- callback 
 
59
        try:
 
60
            result = handleResult(result)
 
61
        except:
 
62
            result = failure.Failure()
 
63
    else:                                       # ---- errback
 
64
        pass
 
65
 
 
66
 
 
67
    if not isinstance(result, failure.Failure): # ---- callback
 
68
        pass
 
69
    else:                                       # ---- errback
 
70
        try:
 
71
            result = handleFailure(result)
 
72
        except:
 
73
            result = failure.Failure()
 
74
 
 
75
 
 
76
 
 
77
def deferredExample():
 
78
    d = defer.Deferred()
 
79
    d.addCallback(failAtHandlingResult)
 
80
    d.addCallback(handleResult)
 
81
    d.addErrback(handleFailure)
 
82
 
 
83
    d.callback("success")
 
84
 
 
85
 
 
86
if __name__ == '__main__':
 
87
    behindTheScenes("success")
 
88
    print "\n-------------------------------------------------\n"
 
89
    Counter.num = 0
 
90
    deferredExample()
 
91