[Twisted-Python] Using flow package to control workflow

Clark C. Evans cce at clarkevans.com
Tue Aug 19 13:46:55 MDT 2003


On Tue, Aug 19, 2003 at 02:20:01PM +0200, mkimpenhaus at plus.de wrote:
| Is it possible to use flow to synchronize that async thingie??

You can use Deferreds within a 'flow' generator, here is an example...

    from __future__ import generators
    from twisted.internet import defer, reactor
    from twisted.flow import flow
    
    def funcThatReturnsDeferred(immediate):
        d = defer.Deferred()
        if immediate:
            d.callback("immediate value")
        else:
            reactor.callLater(1, lambda: d.callback("delayed value"))
        return d 
    
    def myflow():
        #
        # To use a 'deferred' within a generator, it must first
        # be wrapped, then yield, and then use next() on the wrapper.
        #
        immediate = flow.wrap(funcThatReturnsDeferred(True))
        yield immediate
        print immediate.next()
    
        delayed = flow.wrap(funcThatReturnsDeferred(False))
        yield delayed
        print delayed.next()
   
    d = flow.Deferred(myflow)
    d.addCallback(lambda _: reactor.stop())
    reactor.run()

| Next, what should I use -> I tried to use flow.Block() but now
| the whole script seem to hang?

Use flow.Deferred() instead, which in effect takes the results
yielded by the flow and returns them in a list.

| I would like to give an example but this would go beyond the scope...

I hope the above can help.  

Best,

Clark




More information about the Twisted-Python mailing list