tubes.routing module documentationtubes
A Router receives
items with addressing information and dispatches them to an appropriate
output, stripping the addressing information off.
Use like so:
from tubes.tube import receiver, series
from tubes.routing import Router, to
aRouter = Router()
evens = aRouter.newRoute()
odds = aRouter.newRoute()
@receiver()
def evenOdd(item):
if (item % 2) == 0:
yield to(evens, item)
else:
yield to(odds, item)
numbers.flowTo(series(evenOdd, aRouter.drain))
Assuming numbers is a fount of counting integers, this
creates two founts: evens and odds, whose outputs
are even and odd integers, respectively. Note that evenOdd
also uses evens and odds as addresses; the
first argument to to says
where the value will go.
Why do this rather than just having evenOdd just call
methods directly based on whether a number is even or odd?
By using a Router,
flow control relationships are automatically preserved by the same
mechanism that tubes usually use. The distinct drains of
evens and odds can both independently pause their
founts, and the pause state will be propagated to the "numbers"
fount. If you want to send on outputs to multiple drains which may have
complex flow-control interrelationships, you can't do that by calling the
receive method directly since any one of those methods might
reentrantly pause its fount.
| Class | Routed | A Routed is a
specification describing another specification that has been wrapped in a
to. As such, it is an incomplete implementation of ISpecification. |
| Function | to | Construct a provider of Routed(providedBy(where)). |
| Class | Router | A drain with multiple founts that consumes Routed(IX)
from its input and produces IX to its outputs. |
| Class | _To | An object destined for a specific destination. |
Construct a provider of Routed(providedBy(where)).
| Parameters | where | A fount returned from Router.newRoute.
This must be exactly the return value of Router.newRoute,
as it is compared by object identity and not by any feature of IFount. |
| what | the value to deliver. | |
| Returns | a Routed object. | |
| See Also | tubes.routing | |