1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__='''Step
15
16 Utility for chaining defered actions.
17
18 '''
19
20 from twisted.internet import reactor
21 import twisted.internet.defer as defer
22
24 ''' Step through iterable looking for deferreds. Whenever a deferred is
25 encountered wait until it is called before continuing through iterable.
26
27 Iterable is usually a generator function that yields a defered
28 anytime it wants to wait for that deferred to trigger before
29 continuing execution.
30 '''
31 def doSteps(result):
32
33
34
35
36
37
38
39
40
41
42 for result in iterable:
43
44
45
46 if isinstance(result, defer.Deferred):
47 result.addCallback(doSteps)
48 return result
49 finalD.callback(result)
50 return result
51
52
53 if not hasattr(iterable, 'next'):
54 if hasattr(iterable, '__iter__'):
55 iterable = iter(iterable)
56 else:
57 raise 'Must pass an iterable object to step'
58
59
60 finalD = defer.Deferred()
61
62
63 doSteps(None)
64
65 return finalD
66
67
69
71 d = defer.Deferred()
72 reactor.callLater(2, d.callback, n)
73 return d
74
75
77 d1 = self.foo(1)
78 print 'yielding d1 from sequence'
79 yield d1
80 r1 = d1.result
81 d2 = self.foo(r1 + 1)
82 print 'yielding d2 from sequence'
83 yield d2
84 r2 = d2.result
85 print 'yielding final result from sequence'
86 yield r2
87
88
90 print 'final deferred returned %s' % `r`
91
92
98