~blamar/+junk/openstack-api-arrrg

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/howto/listings/deferred/deferred_ex8.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
class Counter(object):
 
11
    num = 0
 
12
    let = 'a'
 
13
 
 
14
    def incrLet(cls):
 
15
        cls.let = chr(ord(cls.let) + 1)
 
16
    incrLet = classmethod(incrLet)
 
17
       
 
18
 
 
19
def handleFailure(f):
 
20
    print "errback"
 
21
    print "we got an exception: %s" % (f.getTraceback(),)
 
22
    return f
 
23
 
 
24
def subCb_B(result):
 
25
    print "sub-callback %s" % (Counter.let,)
 
26
    Counter.incrLet()
 
27
    s = " beautiful!"
 
28
    print "\tadding %r to result" % (s,)
 
29
    result += s
 
30
    return result
 
31
 
 
32
def subCb_A(result):
 
33
    print "sub-callback %s" % (Counter.let,)
 
34
    Counter.incrLet()
 
35
    s = " are "
 
36
    print "\tadding %r to result" % (s,)
 
37
    result += s
 
38
    return result
 
39
 
 
40
def mainCb_1(result):
 
41
    Counter.num += 1
 
42
    print "callback %s" % (Counter.num,)
 
43
    print "\tgot result: %s" % (result,)
 
44
    result += " Deferreds "
 
45
 
 
46
    d = defer.Deferred().addCallback(subCb_A
 
47
                       ).addCallback(subCb_B)
 
48
    d.callback(result)
 
49
    return d
 
50
 
 
51
def mainCb_2(result):
 
52
    Counter.num += 1
 
53
    print "callback %s" % (Counter.num,)
 
54
    print "\tgot result: %s" % (result,)
 
55
    
 
56
 
 
57
def deferredExample():
 
58
    d = defer.Deferred().addCallback(mainCb_1
 
59
                       ).addCallback(mainCb_2)
 
60
 
 
61
    d.callback("I hope you'll agree: ")
 
62
 
 
63
 
 
64
if __name__ == '__main__':
 
65
    deferredExample()
 
66