Implementation:1kbgz Tributary Streaming Graph Execution
| Knowledge Sources | |
|---|---|
| Domains | Streaming, Graph_Execution |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete streaming graph execution engine that runs async node graphs level-by-level using asyncio, provided by the Tributary library.
Description
The StreamingGraph class wraps a graph of connected streaming Node instances and executes them via the asyncio event loop. It performs a deep BFS traversal to order nodes into levels, then executes each level's nodes concurrently with asyncio.gather. The run module-level helper constructs a StreamingGraph from a node and delegates to StreamingGraph.run.
Execution supports blocking (synchronous) and non-blocking (threaded event loop) modes. On start, all node _onstarts coroutines are gathered; on completion, all _onstops coroutines are gathered.
Usage
Import run when you have a fully wired streaming graph and need to execute it. Import StreamingGraph when you need direct control over graph construction, node inspection, or visualization.
Code Reference
Source Location
- Repository: Tributary
- File: tributary/streaming/graph.py (StreamingGraph), tributary/streaming/__init__.py (run helper)
Signature
class StreamingGraph(object):
def __init__(self, node):
"""Initialize graph from output node, collect all nodes via deep BFS."""
def getNodes(self):
"""Traverse graph and collect onstarts/onstops coroutines."""
def stop(self):
"""Signal the graph to stop execution."""
async def _run(self):
"""Internal async execution loop: run level-by-level until StreamEnd."""
def run(self, blocking=True, newloop=False, start=True):
"""Execute the graph, optionally blocking until completion.
Args:
blocking: If True, block until graph completes. Returns last value.
newloop: If True, create a new asyncio event loop.
start: If True and non-blocking, start a daemon thread with the loop.
"""
def graphviz(self):
"""Return a graphviz.Digraph of the graph."""
def dagre(self):
"""Return an ipydagred3.DagreD3Widget of the graph."""
def run(node, blocking=True, **kwargs):
"""Module-level helper: construct StreamingGraph and run it."""
Import
import tributary.streaming as ts
# or:
from tributary.streaming import run
from tributary.streaming.graph import StreamingGraph
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| node | Node | Yes | The output/starting node of the wired graph |
| blocking | bool | No | Block until completion (default True) |
| newloop | bool | No | Create a new asyncio event loop (default False) |
| start | bool | No | Start daemon thread for non-blocking mode (default True) |
Outputs
| Name | Type | Description |
|---|---|---|
| run (blocking) | any | The last value produced by the starting node |
| run (non-blocking) | asyncio.AbstractEventLoop | The running event loop |
Usage Examples
Blocking Execution
import tributary.streaming as ts
# Build a simple pipeline
source = ts.Const(value=42, count=3)
doubled = source * 2
out = ts.Print(doubled)
# Run synchronously
result = ts.run(out)
print("Final value:", result)
Non-Blocking Execution
import tributary.streaming as ts
import asyncio
# Build a pipeline
source = ts.Random(count=100, interval=0.5)
out = ts.Print(source)
# Run in background (non-blocking)
loop = ts.run(out, blocking=False)
# ... do other work while the stream runs ...
# Stop when done:
loop.call_soon_threadsafe(loop.stop)
Direct StreamingGraph Usage
import tributary.streaming as ts
from tributary.streaming.graph import StreamingGraph
source = ts.Curve(value=[1, 2, 3, 4, 5])
squared = source * source
collected = ts.Collect(squared)
graph = StreamingGraph(collected)
graph.run(blocking=True)
print(collected.value()) # [1, 4, 9, 16, 25]