Skip to content

Commit

Permalink
Restore example basic
Browse files Browse the repository at this point in the history
  • Loading branch information
Snarr committed Nov 27, 2024
1 parent 08e1e49 commit 8e3cfbc
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions sdk-python/examples/basic/example_basic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
import logging
from pathlib import Path
import random

import littlehorse
from littlehorse.config import LHConfig
from littlehorse.model import VariableType, VariableMutationType
from littlehorse.model import VariableType
from littlehorse.worker import LHTaskWorker, WorkerContext
from littlehorse.workflow import WorkflowThread, Workflow

Expand All @@ -18,32 +19,31 @@ def get_config() -> LHConfig:
config.load(config_path)
return config

def test_workflow() -> Workflow:
def my_entrypoint(wf: WorkflowThread) -> None:
user_id = wf.add_variable("user-id", VariableType.STR).required()
item = wf.add_variable("item", VariableType.STR).required()
quantity = wf.add_variable("quantity", VariableType.INT).required()

wf.execute("charge-credit-card", user_id, quantity.multiply(wf.execute("fetch-price"), item))
def get_workflow() -> Workflow:
def my_entrypoint(wf: WorkflowThread) -> None:
the_name = wf.add_variable("input-name", VariableType.STR)
wf.execute("greet", the_name)

return Workflow("example-basic", my_entrypoint)

async def fetch_price(item: str, ctx: WorkerContext) -> str:
return 5

async def charge_credit_card(user_id: str, total_price: float, ctx: WorkerContext) -> str:
print(f"Charging {user_id} ${total_price}")
async def greeting(name: str, ctx: WorkerContext) -> str:
msg = f"Hello {name}!. WfRun {ctx.wf_run_id.id}"
print(msg)
await asyncio.sleep(random.uniform(0.5, 1.5))
return msg


async def main() -> None:
config = get_config()
wf = test_workflow()
wf = get_workflow()

littlehorse.create_task_def(fetch_price, "fetch-price", config)
littlehorse.create_task_def(charge_credit_card, "charge-credit-card", config)
littlehorse.create_task_def(greeting, "greet", config)
littlehorse.create_workflow_spec(wf, config)

await littlehorse.start(LHTaskWorker(fetch_price, "fetch-price", config), LHTaskWorker(charge_credit_card, "charge-credit-card", config))
await littlehorse.start(LHTaskWorker(greeting, "greet", config))


if __name__ == "__main__":
asyncio.run(main())
asyncio.run(main())

0 comments on commit 8e3cfbc

Please sign in to comment.