-
Notifications
You must be signed in to change notification settings - Fork 0
How To Use Context
Use Context to carry scoped execution data that should be available to nodes, pipelines, and runners without storing that data as final runtime output.
Typical context values include:
- stage names
- route selectors
- modes
- configuration flags
- environment-like execution settings
graphk.Context is a scoped Store.
It behaves like hierarchical state, but its role is different from Session:
-
Contextis configuration -
Sessionis runtime result state
Use context for values that shape execution. Use session for values produced by execution.
GraphK contexts can attach to each other through parent relationships. This creates a context chain.
Typical resolution chain during execution:
SessionNodePipelineRunnerProgram
This means a node can access values coming from different levels with GraphK's scoped key syntax.
Examples:
-
modeSearch upward through the chain. -
@Node/stageRead the nearest node-level context value namedstage. -
@Pipeline/routeRead the nearest pipeline-level context value namedroute. -
@Runner/modeRead a runner-level execution setting. -
@Program/modeRead a program-level shared execution setting.
It lets you:
- avoid passing the same parameters through many method calls
- keep local configuration local
- override higher-level values at lower levels when needed
- route execution based on scoped state
import graphk
node = graphk.demo.IncrementNode(
"A",
increment=1,
target_key="counter",
context=graphk.Context(stage="node-stage"),
)
pipeline = graphk.SequencePipe(
nodes=[node],
context=graphk.Context(route="alpha"),
)
program = graphk.Program({"main": pipeline}, context=graphk.Context(mode="strict"))
session = graphk.Session(counter=0)
emitter = graphk.Emitter(program)
emitter.request("main", session)
print(session.get("@Node/stage"))
print(session.get("@Pipeline/route"))
print(session.get("@Runner/mode"))
print(session.get("@Program/mode"))GraphK attaches contexts when parent/child objects are connected.
Typical behavior:
- a node context attaches to its pipeline context
- a pipeline context attaches to its parent pipeline, runner, or program context
- a session attaches to the current node or runner context during execution
This means context resolution is dynamic and execution-aware.
Give each node a local stage:
graphk.Context(stage="validate")Give a pipeline a route name:
graphk.Context(route="alpha")Give a runner a high-level mode:
graphk.Context(mode="strict")- Do not store final results in
Contextwhen they belong inSession. - Do not use global variables for configuration that should be scoped.
- Do not forget that unscoped
get("value")resolves upward through the chain.
Recommended references: