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.
Class _String Wrapper for a string object; don't create directly use flow.wrap
Class _List Wrapper for lists and tuple objects; don't create directly
Class _DeferredInstruction Undocumented
Class _Iterable Wrapper for iterable objects, pass in a next() function
Class _Deferred Wraps a Deferred object into a stage; create with flow.wrap
Function wrap Wraps various objects for use within a flow
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.