Part of twisted.flow.threads View Source View In Hierarchy
A stage which runs a blocking iterable in a separate thread
This stage tunnels output from an iterable executed in a separate thread to the main thread. This process is carried out by a result buffer, and returning Cooperate if the buffer is empty. The wrapped iterable's __iter__ and next() methods will only be invoked in the spawned thread.
This can be used in one of two ways, first, it can be extended via inheritance; with the functionality of the inherited code implementing next(), and using init() for initialization code to be run in the thread.
If the iterable happens to have a chunked attribute, and that attribute is true, then this wrapper will assume that data arrives in chunks via a sequence instead of by values.
For example:from __future__ import generators from twisted.internet import reactor, defer from twisted.flow import flow from twisted.flow.threads import Threaded def countSleep(index): from time import sleep for index in range(index): sleep(.3) print "sleep", index yield index def countCooperate(index): for index in range(index): yield flow.Cooperate(.1) print "cooperate", index yield "coop %s" % index d = flow.Deferred( flow.Merge( Threaded(countSleep(5)), countCooperate(5))) def prn(x): print x reactor.stop() d.addCallback(prn) reactor.run()
Class | Instruction | Undocumented |
Method | __init__ | Undocumented |
Method | _process_result | Undocumented |
Method | _stopping | Undocumented |
Method | _process | Undocumented |
Method | _yield | executed during a yield statement by previous stage |
Inherited from Stage:
Method | __iter__ | Undocumented |
Method | next | return current result |
executed during a yield statement by previous stage
This method is private within the scope of the flow module, it is used by one stage in the flow to ask a subsequent stage to produce its value. The result of the yield is then stored in self.result and is an instance of Failure if a problem occurred.