Files
hatchet/sdks/python/examples/fanout/stream.py
Matt Kaye 5062bf1e3e V1 SDKs and Docs (#1361)
New SDKs and docs for the v1 release.
2025-03-25 15:45:07 -07:00

39 lines
1004 B
Python

import asyncio
import random
from examples.fanout.worker import ParentInput, parent_wf
from hatchet_sdk import Hatchet
from hatchet_sdk.clients.admin import TriggerWorkflowOptions
async def main() -> None:
hatchet = Hatchet()
# Generate a random stream key to use to track all
# stream events for this workflow run.
streamKey = "streamKey"
streamVal = f"sk-{random.randint(1, 100)}"
# Specify the stream key as additional metadata
# when running the workflow.
# This key gets propagated to all child workflows
# and can have an arbitrary property name.
parent_wf.run(
ParentInput(n=2),
options=TriggerWorkflowOptions(additional_metadata={streamKey: streamVal}),
)
# Stream all events for the additional meta key value
listener = hatchet.listener.stream_by_additional_metadata(streamKey, streamVal)
async for event in listener:
print(event.type, event.payload)
if __name__ == "__main__":
asyncio.run(main())