Class Threaded
Instruction
--+
|
Stage
--+
|
Threaded
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.
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()
Method Summary |
|
__init__(self,
iterable,
*trap)
|
|
_process(self)
|
|
_process_result(self,
val)
|
|
_stopping(self)
|
|
_yield (self)
executed during a yield statement by previous stage |
Inherited from Stage |
|
__iter__ (self)
|
|
next (self)
return current result |
_yield(self)
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.
-
- Overrides:
twisted.flow.base.Stage._yield (inherited documentation)
|