Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,6 @@ cython_debug/

# Generated files
openapi.json
src/workflows/openapi_generated_client/README.md
src/workflows/openapi_generated_client/pyproject.toml
src/workflows/openapi_generated_client/.gitignore
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ repos:
rev: v0.23.1
hooks:
- id: toml-sort-fix

exclude: ^(src/workflows/openapi_generated_client/)
54 changes: 54 additions & 0 deletions examples/client/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import asyncio

from workflows.client.client import WorkflowClient

from workflows.events import StartEvent
from pydantic import Field

from typing import Literal


class InputNumbers(StartEvent):
a: int
b: int
operation: Literal["sum", "subtraction"] = Field(default="sum")


async def main() -> None:
client = WorkflowClient(protocol="http", host="localhost", port=8000)
workflows = await client.list_workflows()
print("===== AVAILABLE WORKFLOWS ====")
print(workflows)
is_healthy = await client.is_healthy()
print("==== HEALTH CHECK ====")
print("Healthy" if is_healthy else "Not Healty :(")
ping_time = await client.ping()
print("==== PING TIME ====")
print(ping_time, "ms")
handler = await client.run_workflow_nowait(
"add_or_subtract",
start_event=InputNumbers(a=1, b=3, operation="sum"),
context=None,
)
print("==== STARTING THE WORKFLOW ===")
print(f"Workflow running with handler: {handler}")
print("=== STREAMING EVENTS ===")
async for event in client.get_workflow_events(handler):
print("Received data:", event)
# Poll for result
result = handler.status.value
while result == "running":
try:
result = await client.get_workflow_result(handler)
if result != "running":
break
await asyncio.sleep(1)
except Exception as e:
print(f"Error: {e}")
await asyncio.sleep(1)

print(f"Final result: {result}")


if __name__ == "__main__":
asyncio.run(main())
61 changes: 61 additions & 0 deletions examples/client/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from workflows import Workflow, step, Context
from workflows.events import StartEvent, StopEvent, Event
from pydantic import Field
from workflows.server import WorkflowServer

from typing import Literal


class InputNumbers(StartEvent):
a: int
b: int
operation: Literal["sum", "subtraction"] = Field(default="sum")


class CalculationEvent(Event):
result: int


class OutputEvent(StopEvent):
message: str


class AddOrSubtractWorkflow(Workflow):
@step
async def first_step(
self, ev: InputNumbers, ctx: Context
) -> CalculationEvent | None:
ctx.write_event_to_stream(ev)
result = ev.a + ev.b if ev.operation == "sum" else ev.a - ev.b
async with ctx.store.edit_state() as state:
state.operation = ev.operation
state.a = ev.a
state.b = ev.b
state.result = result
ctx.write_event_to_stream(CalculationEvent(result=result))
return CalculationEvent(result=result)

@step
async def second_step(self, ev: CalculationEvent, ctx: Context) -> OutputEvent:
state = await ctx.store.get_state()
return OutputEvent(
message=f"You approved the result from your operation ({state.operation}) between {state.a} and {state.b}: {ev.result}"
)


async def main() -> None:
server = WorkflowServer()
server.add_workflow("add_or_subtract", AddOrSubtractWorkflow(timeout=1000))
server.add_workflow("add_or_subtract_2", AddOrSubtractWorkflow(timeout=1000))
try:
await server.serve("localhost", 8000)
except KeyboardInterrupt:
return
except Exception as e:
raise ValueError(f"An error occurred: {e}")


if __name__ == "__main__":
import asyncio

asyncio.run(main())
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies = [

[project.optional-dependencies]
server = ["starlette>=0.39.0", "uvicorn>=0.32.0"]
client = ["httpx>=0.28.1"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should bound this to <1. Looks like they're working on some breaking changes


[tool.basedpyright]
typeCheckingMode = "standard"
Expand Down
3 changes: 3 additions & 0 deletions src/workflows/client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .client import WorkflowClient

__all__ = ["WorkflowClient"]
Loading