Files
hatchet/examples/python/fanout/worker.py
matt cca0999eea [Python]: Fixing logging bugs, fixing duped sleep key bug (#2040)
* feat: add flag to disable log capture

* fix: sleep bug with duped key

* fix: allow formatters to be passed through

* feat: support filters too

* fix: cruft

* chore: gen

* feat: changelog

* fix: lint

* [Python] Fix: Don't retry gRPC requests on 4xx (#2024)

* fix: dont retry on 4xx

* chore: ver

* fix: sleep conditions with index

* fix: bug in sleep conditions

* chore: gen
2025-07-23 15:15:31 -04:00

71 lines
1.5 KiB
Python

from datetime import timedelta
from typing import Any
from pydantic import BaseModel
from hatchet_sdk import Context, Hatchet, TriggerWorkflowOptions
hatchet = Hatchet(debug=True)
# > FanoutParent
class ParentInput(BaseModel):
n: int = 100
class ChildInput(BaseModel):
a: str
parent_wf = hatchet.workflow(name="FanoutParent", input_validator=ParentInput)
child_wf = hatchet.workflow(name="FanoutChild", input_validator=ChildInput)
@parent_wf.task(execution_timeout=timedelta(minutes=5))
async def spawn(input: ParentInput, ctx: Context) -> dict[str, Any]:
print("spawning child")
result = await child_wf.aio_run_many(
[
child_wf.create_bulk_run_item(
input=ChildInput(a=str(i)),
options=TriggerWorkflowOptions(
additional_metadata={"hello": "earth"}, key=f"child{i}"
),
)
for i in range(input.n)
],
)
print(f"results {result}")
return {"results": result}
# > FanoutChild
@child_wf.task()
async def process(input: ChildInput, ctx: Context) -> dict[str, str]:
print(f"child process {input.a}")
return {"status": input.a}
@child_wf.task(parents=[process])
async def process2(input: ChildInput, ctx: Context) -> dict[str, str]:
process_output = ctx.task_output(process)
a = process_output["status"]
return {"status2": a + "2"}
def main() -> None:
worker = hatchet.worker("fanout-worker", slots=40, workflows=[parent_wf, child_wf])
worker.start()
if __name__ == "__main__":
main()