Changes between Version 1 and Version 2 of DeferredGenerator


Ignore:
Timestamp:
08/31/2006 01:02:25 PM (9 years ago)
Author:
yang
Comment:

minor fixes in code snippet

Legend:

Unmodified
Added
Removed
Modified
  • DeferredGenerator

    v1 v2  
    1010#!python
    1111
    12     from twisted.internet import defer
    13    
    14     # The reactor is running and we are going to obtain a Deferred "d"
    15 
    16     def getSomeDeferred():
    17          """Some function that returns a Deferred."""
    18          d = defer.Deferred()
    19          reactor.callLater(2, d.callback, 'A string that yells "foo!"')
    20          return d
    21    
    22     def anotherDeferred(needle, haystack):
    23          """Some other function that returns a Deferred."""
    24          d = defer.Deferred()
    25          reactor.callLater(2, d.callback, haystack.find(needle))
    26          return d
    27    
    28     @defer.deferredGenerator
    29     def find(needle):
    30          """A Deferred generator function"""
    31          print "I am going to find %s in a haystack." % needle
    32          # After yielding waitForDeferred, our generator
    33          # will be put on hold for a while.
    34          wfd = defer.waitForDeferred(getSomeDeferred())
    35          yield wfd
    36          # The reactor will call .next(), and we resume here.
    37          haystack = wfd.getResult()
    38          print "I got my haystack back from a deferred."
    39          # We're going to wait for another deferred result.
    40          wfd = defer.waitForDeferred(anotherDeferred(needle, haystack))
    41          yield wfd
    42          # When we get our next result, the procedure resumes here.
    43          print "I found %s at character %d" % (repr(needle), d.getResult())
    44 
    45     # We call the deferred generator like any other function and immediately
    46     # get a Deferred that fires when the generator is done.
    47 
    48     d = find('foo!')
     12from twisted.internet import defer, reactor
     13
     14# The reactor is running and we are going to obtain a Deferred "d"
     15
     16def getSomeDeferred():
     17    """Some function that returns a Deferred."""
     18    d = defer.Deferred()
     19    reactor.callLater(1, d.callback, 'A string that yells "foo!"')
     20    return d
     21
     22def anotherDeferred(needle, haystack):
     23    """Some other function that returns a Deferred."""
     24    d = defer.Deferred()
     25    reactor.callLater(1, d.callback, haystack.find(needle))
     26    return d
     27
     28@defer.deferredGenerator
     29def find(needle):
     30    """A Deferred generator function"""
     31    print "I am going to find %s in a haystack." % needle
     32    # After yielding waitForDeferred, our generator
     33    # will be put on hold for a while.
     34    wfd = defer.waitForDeferred(getSomeDeferred())
     35    yield wfd
     36    # The reactor will call .next(), and we resume here.
     37    haystack = wfd.getResult()
     38    print "I got my haystack back from a deferred."
     39    # We're going to wait for another deferred result.
     40    wfd = defer.waitForDeferred(anotherDeferred(needle, haystack))
     41    yield wfd
     42    # When we get our next result, the procedure resumes here.
     43    print "I found %s at character %d" % (repr(needle), wfd.getResult())
     44    reactor.stop()
     45
     46# We call the deferred generator like any other function and immediately
     47# get a Deferred that fires when the generator is done.
     48
     49d = find('foo!')
     50reactor.run()
    4951}}}
    5052