Package twisted :: Package flow :: Module wrap

[frames | no frames]

## Module twisted.flow.wrap

flow.wrap

This module provides the wrap() function in the flow module and the private classes used for its implementation.

Classes
`_Deferred` | Wraps a Deferred object into a stage; create with flow.wrap
`_DeferredInstruction` |
`_Iterable` | Wrapper for iterable objects, pass in a next() function
`_List` | Wrapper for lists and tuple objects; don't create directly
`_String` | Wrapper for a string object; don't create directly use flow.wrap

Function Summary
| ``wrap`(obj, *trap)` Wraps various objects for use within a flow The following example illustrates many different ways in which regular objects can be wrapped by the flow module to behave in a cooperative manner.

Function Details

### wrap(obj, *trap)

    Wraps various objects for use within a flow

    The following example illustrates many different
    ways in which regular objects can be wrapped by
    the flow module to behave in a cooperative manner.

        # required imports
        from __future__ import generators
        from twisted.flow import flow
        from twisted.internet import reactor, defer

        # save this function, it is used everwhere
        def printFlow(source):
            def printer(source):
                source = flow.wrap(source)
                while True:
                    yield source
                    print source.next()
            d = flow.Deferred(printer(source))
            d.addCallback(lambda _: reactor.stop())
            reactor.run()

        source = "string"
        printFlow(source)

        source = ["one",flow.Cooperate(1),"two"]
        printFlow(source)

        def source():
            yield "aeye"
            yield flow.Cooperate()
            yield "capin"
        printFlow(source)

        source = Deferred()
        reactor.callLater(1, lambda: source.callback("howdy"))
        printFlow(source)

Generated by Epydoc 2.0 on Sat May 15 20:07:39 2004 | http://epydoc.sf.net