Files
hatchet/sdks/python/examples/concurrency_limit/worker.py
T
Matt Kaye 0c4e52a97c [Docs] Concurrency, Additional Metadata (#1415)
* fix: child spawning python example

* fix: more things

* feat: additional meta

* feat: concurrency

* fix: proof

* fix: lint

* fix: python migration

* fix: test

* fix: lint

* fix: link on migration guide

---------

Co-authored-by: Alexander Belanger <alexander@hatchet.run>
2025-03-26 18:34:02 -07:00

52 lines
974 B
Python

import time
from typing import Any
from pydantic import BaseModel
from hatchet_sdk import (
ConcurrencyExpression,
ConcurrencyLimitStrategy,
Context,
Hatchet,
)
hatchet = Hatchet(debug=True)
# ❓ Workflow
class WorkflowInput(BaseModel):
run: int
group_key: str
concurrency_limit_workflow = hatchet.workflow(
name="ConcurrencyDemoWorkflow",
concurrency=ConcurrencyExpression(
expression="input.group_key",
max_runs=5,
limit_strategy=ConcurrencyLimitStrategy.CANCEL_IN_PROGRESS,
),
input_validator=WorkflowInput,
)
# ‼️
@concurrency_limit_workflow.task()
def step1(input: WorkflowInput, ctx: Context) -> dict[str, Any]:
time.sleep(3)
print("executed step1")
return {"run": input.run}
def main() -> None:
worker = hatchet.worker(
"concurrency-demo-worker", slots=10, workflows=[concurrency_limit_workflow]
)
worker.start()
if __name__ == "__main__":
main()