Module t.f.wrap

Part of twisted.flow View Source

flow.wrap

This module provides the wrap() function in the flow module and the private classes used for its implementation.
Line # Kind Name Docs
144 Function wrap Wraps various objects for use within a flow
18 Class _String Wrapper for a string object; don't create directly use flow.wrap
32 Class _List Wrapper for lists and tuple objects; don't create directly
52 Class _DeferredInstruction Undocumented
58 Class _Iterable Wrapper for iterable objects, pass in a next() function
112 Class _Deferred Wraps a Deferred object into a stage; create with flow.wrap
def wrap(obj, *trap): (source)

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.

For example:
   # 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)
API Documentation for Twisted, generated by pydoctor at 2008-10-26 16:12:37.