Skip to content

How To Execute Multiple Routes with MultiPipe

Fernando Koch edited this page Mar 30, 2026 · 2 revisions

How To Execute Multiple Routes With MultiPipe

Purpose

Use MultiPipe when all matching branches should execute instead of selecting only one.

This is the GraphK tool for fan-out behavior across multiple matching routes.

What Does MultiPipe Do?

graphk.MultiPipe extends BranchPipe, but instead of choosing one matching branch, it:

  1. resolves all matching branch indices
  2. orders the selected matches
  3. executes every selected branch in that order

When Should You Use It?

Good use cases:

  • run several matching handlers
  • apply multiple route-specific transformations
  • execute parallel-in-concept branches sequentially in one pipeline
  • fan out work to all matching route groups

Use MultiPipe when many routes should run. Use BranchPipe when only one route should run.

Order Policies

Important constants:

  • MultiPipe.ORDER_STORED
  • MultiPipe.ORDER_RANDOM

ORDER_STORED

Use when:

  • execution should follow branch definition order
  • behavior should remain deterministic

ORDER_RANDOM

Use when:

  • execution order can vary intentionally
  • or when randomness is seeded for controlled demonstration

Minimal Example

import graphk


pipe = graphk.MultiPipe(
    order_policy=graphk.MultiPipe.ORDER_STORED,
    nodes=[
        (
            graphk.Matcher(task="route"),
            graphk.SequencePipe(nodes=[graphk.demo.RouteNode("A")]),
        ),
        (
            graphk.Matcher(task="route"),
            graphk.SequencePipe(nodes=[graphk.demo.RouteNode("B")]),
        ),
    ],
)

pipeline = graphk.SequencePipe(nodes=[pipe])
session = graphk.Session(task="route")
runner = graphk.Runner(pipeline, session)

runner.start()
runner.run()

print(session.to_dict())

Typical Use Cases

Multi-Handler Processing

Apply all matching route handlers to a single request.

Ordered Fan-Out

Run several transformations in a defined order but still model them as separate branches.

Scenario Composition

Model several optional workflows that should all run when their matchers pass.

BranchPipe vs MultiPipe

Use BranchPipe when:

  • one route should win
  • routing is exclusive

Use MultiPipe when:

  • every matching route should run
  • routing is cumulative

Common Mistakes

  • Do not use MultiPipe when only one route is supposed to execute.
  • Do not assume ORDER_RANDOM is stable unless randomness is explicitly seeded.
  • Do not forget that each selected item is still expected to be a pipeline branch.

Learn More

Recommended references:

Clone this wiki locally