mirror of
https://github.com/hatchet-dev/hatchet.git
synced 2026-01-06 08:49:53 -06:00
Fe overhaul docs (#1640)
* api changes * doc changes * move docs * generated * generate * pkg * backmerge main * revert to main * revert main * race? * remove go tests
This commit is contained in:
13
examples/python/concurrency_limit_rr_load/event.py
Normal file
13
examples/python/concurrency_limit_rr_load/event.py
Normal file
@@ -0,0 +1,13 @@
|
||||
import random
|
||||
|
||||
from hatchet_sdk import Hatchet
|
||||
|
||||
hatchet = Hatchet(debug=True)
|
||||
|
||||
# Create a list of events with desired distribution
|
||||
events = ["1"] * 10000 + ["0"] * 100
|
||||
random.shuffle(events)
|
||||
|
||||
# Send the shuffled events
|
||||
for group in events:
|
||||
hatchet.event.push("concurrency-test", {"group": group})
|
||||
77
examples/python/concurrency_limit_rr_load/worker.py
Normal file
77
examples/python/concurrency_limit_rr_load/worker.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import random
|
||||
import time
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from hatchet_sdk import (
|
||||
ConcurrencyExpression,
|
||||
ConcurrencyLimitStrategy,
|
||||
Context,
|
||||
Hatchet,
|
||||
)
|
||||
|
||||
hatchet = Hatchet(debug=True)
|
||||
|
||||
|
||||
class LoadRRInput(BaseModel):
|
||||
group: str
|
||||
|
||||
|
||||
load_rr_workflow = hatchet.workflow(
|
||||
name="LoadRoundRobin",
|
||||
on_events=["concurrency-test"],
|
||||
concurrency=ConcurrencyExpression(
|
||||
expression="input.group",
|
||||
max_runs=1,
|
||||
limit_strategy=ConcurrencyLimitStrategy.GROUP_ROUND_ROBIN,
|
||||
),
|
||||
input_validator=LoadRRInput,
|
||||
)
|
||||
|
||||
|
||||
@load_rr_workflow.on_failure_task()
|
||||
def on_failure(input: LoadRRInput, context: Context) -> dict[str, str]:
|
||||
print("on_failure")
|
||||
return {"on_failure": "on_failure"}
|
||||
|
||||
|
||||
@load_rr_workflow.task()
|
||||
def step1(input: LoadRRInput, context: Context) -> dict[str, str]:
|
||||
print("starting step1")
|
||||
time.sleep(random.randint(2, 20))
|
||||
print("finished step1")
|
||||
return {"step1": "step1"}
|
||||
|
||||
|
||||
@load_rr_workflow.task(
|
||||
retries=3,
|
||||
backoff_factor=5,
|
||||
backoff_max_seconds=60,
|
||||
)
|
||||
def step2(sinput: LoadRRInput, context: Context) -> dict[str, str]:
|
||||
print("starting step2")
|
||||
if random.random() < 0.5: # 1% chance of failure
|
||||
raise Exception("Random failure in step2")
|
||||
time.sleep(2)
|
||||
print("finished step2")
|
||||
return {"step2": "step2"}
|
||||
|
||||
|
||||
@load_rr_workflow.task()
|
||||
def step3(input: LoadRRInput, context: Context) -> dict[str, str]:
|
||||
print("starting step3")
|
||||
time.sleep(0.2)
|
||||
print("finished step3")
|
||||
return {"step3": "step3"}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
worker = hatchet.worker(
|
||||
"concurrency-demo-worker-rr", slots=50, workflows=[load_rr_workflow]
|
||||
)
|
||||
|
||||
worker.start()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user