-
Notifications
You must be signed in to change notification settings - Fork 0
How To Use an Emitter
Use Emitter when you want GraphK to behave like a request/response dispatcher over a registered set of pipelines instead of a manually managed runner.
Emitter works with a Program:
-
Programstores pipelines by id -
Emitterselects one pipeline by id or by program default -
Runneris created transiently for each request
Use Emitter when:
- you want to expose several pipelines behind one public interface
- you want pipeline storage separated from execution
- you want a request/response API that feels simpler than managing
Runnerdirectly
Prefer Runner when you need lower-level execution control over one pipeline.
Purpose:
- resolve a pipeline id from the program
- create a session if one is not provided
- execute the selected pipeline through a transient runner
- cache the resulting session
- return that session
Behavior:
- if
idis omitted and the program has one pipeline, that pipeline is used - if
idis omitted and the program has a configured default, that pipeline is used - if the id is unknown, or dispatch is ambiguous, an exception is raised
- the emitter does not keep or reuse runner instances
Purpose:
- read the current response payload from the cached session
Purpose:
- replace the cached session
Purpose:
- enable logging on the transient runners created for requests
import graphk
pipeline = graphk.SequencePipe(nodes=[graphk.demo.EchoNode()])
program = graphk.Program({"echo": pipeline})
emitter = graphk.Emitter(program)
emitter.request("echo", graphk.Session(value=5))
print(emitter.response())import graphk
pipeline = graphk.SequencePipe(nodes=[graphk.demo.EchoNode()])
program = graphk.Program({"echo": pipeline})
emitter = graphk.Emitter(program)
session = emitter.request(session=graphk.Session(value=5))
print(session.to_dict())
print(emitter.response())This works because the program has only one registered pipeline.
import graphk
pipeline = graphk.SequencePipe(nodes=[graphk.demo.EchoNode()])
program = graphk.Program({"echo": pipeline})
emitter = graphk.Emitter(program)
emitter.enable_log(graphk.Logger.DEBUG)
session = emitter.request("echo", graphk.Session(value=5))
print(session.to_dict())
print(emitter.response())import graphk
base = graphk.Session(prefix="base", counter=10, value=3)
pipeline = graphk.SequencePipe(
nodes=[
graphk.demo.ApplyNode(
"PrefixEcho",
fn=lambda session: f"{session.get('prefix', 'ok')}:{session.get('value', 0)}",
response_key="response",
)
]
)
program = graphk.Program({"prefix": pipeline})
emitter = graphk.Emitter(program)
session = emitter.request("prefix", base)
print(session is base)
print(emitter.response())By default, the emitter reads the response payload from the cached session key:
- response key:
response
Nodes in the selected pipeline should write their final payload into response if they are intended to feed the emitter response directly.
Good built-in options:
EchoNodeApplyNodeIncrementNode(..., response=True)ConcatNode(..., response=True)RouteNode
Internally, Emitter is a convenience dispatcher that creates a fresh Runner for each call to request(...).
Use Emitter when:
- you want the package to feel like a reusable service
- you want dispatch by pipeline id
- you want one method call per request
Use Runner when:
- you need direct start/step/run control
- you want to inspect execution at a lower level
- Do not initialize
Emitterwith a raw pipeline; initialize it with aProgram. - Do not assume a response appears automatically unless nodes write to
response. - Do not forget that request input now lives in the provided
Session. - Do not omit
idwhen the program has multiple pipelines unless a default pipeline is configured. - Do not reimplement program dispatch when
Emitteralready matches the use case.
Recommended references: