Ticket #1009: comments.txt

File comments.txt, 1.3 KB (added by hypatia, 13 years ago)

Alex Levy's examples with his comments.

Line 
1In case you want it for documentation, here's a version with comments
2added. I just hate updating Movabletype. ;)
3
4--snip--
5
6from twisted.internet import reactor
7from twisted.internet import defer
8
9waitFor = defer.waitForDeferred
10
11def getSomeDeferred():
12     """Some function that returns a Deferred."""
13     d = defer.Deferred()
14     reactor.callLater(2, d.callback, 'A string that yells "foo!"')
15     return d
16
17def anotherDeferred(needle, haystack):
18     """Some other function that returns a Deferred."""
19     d = defer.Deferred()
20     reactor.callLater(2, d.callback, haystack.find(needle))
21     return d
22
23@defer.deferredGenerator
24def find(needle):
25     print 'I am going to find', needle, 'in a haystack.'
26     # After yielding waitForDeferred, our generator
27     # will be put on hold for a while.
28     d = waitFor(getSomeDeferred())
29     yield d
30     # The reactor will call .next(), and we resume here.
31     haystack = d.getResult()
32     print 'I got my haystack back from a deferred.'
33     # We're going to wait for another deferred result.
34     d = waitFor(anotherDeferred(needle, haystack))
35     yield d
36     # When we get our next result, the procedure resumes here.
37     print 'I found', repr(needle), 'at character', d.getResult()
38     return
39
40find('foo!')
41reactor.run()
42