Files
hatchet/sdks/python/examples/dag/worker.py
Nathan Gage 99bcfa1037 add: CoroutineLike & AwaitableLike types (#1713)
* add: CoroutineLike & AwaitableLike types

* fix: type checker

* fix: reduce type narrowing on _parse_task_name

* fix: rm _aio_output helper

* Revert "fix: rm _aio_output helper"

This reverts commit ff51126c43.

* fix: rm _aio_output helper

* chore: version

* feat: add tests for all the standalone run flavors

* refactor: simplify internals a bit

---------

Co-authored-by: mrkaye97 <mrkaye97@gmail.com>
2025-05-14 11:06:14 -04:00

63 lines
1.4 KiB
Python

import random
import time
from datetime import timedelta
from pydantic import BaseModel
from hatchet_sdk import Context, EmptyModel, Hatchet
class StepOutput(BaseModel):
random_number: int
class RandomSum(BaseModel):
sum: int
hatchet = Hatchet(debug=True)
dag_workflow = hatchet.workflow(name="DAGWorkflow")
@dag_workflow.task(execution_timeout=timedelta(seconds=5))
def step1(input: EmptyModel, ctx: Context) -> StepOutput:
return StepOutput(random_number=random.randint(1, 100))
@dag_workflow.task(execution_timeout=timedelta(seconds=5))
async def step2(input: EmptyModel, ctx: Context) -> StepOutput:
return StepOutput(random_number=random.randint(1, 100))
@dag_workflow.task(parents=[step1, step2])
async def step3(input: EmptyModel, ctx: Context) -> RandomSum:
one = ctx.task_output(step1).random_number
two = ctx.task_output(step2).random_number
return RandomSum(sum=one + two)
@dag_workflow.task(parents=[step1, step3])
async def step4(input: EmptyModel, ctx: Context) -> dict[str, str]:
print(
"executed step4",
time.strftime("%H:%M:%S", time.localtime()),
input,
ctx.task_output(step1),
ctx.task_output(step3),
)
return {
"step4": "step4",
}
def main() -> None:
worker = hatchet.worker("dag-worker", workflows=[dag_workflow])
worker.start()
if __name__ == "__main__":
main()