From 6b06b9c52d73d3958b9a00d2aef7a0dfa2ab4cd8 Mon Sep 17 00:00:00 2001 From: Marc Duiker Date: Wed, 7 May 2025 10:38:13 +0200 Subject: [PATCH] Add comments and types Signed-off-by: Marc Duiker --- .../fundamentals/basic/basic_workflow.py | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/tutorials/workflow/python/fundamentals/basic/basic_workflow.py b/tutorials/workflow/python/fundamentals/basic/basic_workflow.py index 42130c7c..ed1a45ca 100644 --- a/tutorials/workflow/python/fundamentals/basic/basic_workflow.py +++ b/tutorials/workflow/python/fundamentals/basic/basic_workflow.py @@ -3,19 +3,37 @@ from dapr.ext.workflow import DaprWorkflowContext, WorkflowActivityContext, Work wf_runtime = WorkflowRuntime() +""" +Workflows orchestrate activities and other (child)workflows, and include business logic (if/else or switch statements). +Workflow code must be be deterministic. Any non-deterministic behavior should be written inside activities. + +Workflow definitions use the `workflow` decorator to define a workflow. +The first argument (`ctx`) is the `DaprWorkflowContext`, this contains properties +about the workflow instance, and methods to call activities. +The second argument (`wf_input`) is the input to the workflow. It can be a simple or complex type. +""" @wf_runtime.workflow(name='basic_workflow') def basic_workflow(ctx: DaprWorkflowContext, wf_input: str): result1 = yield ctx.call_activity(activity1, input=wf_input) result2 = yield ctx.call_activity(activity2, input=result1) - return result2 +""" +Activity code typically performs a one specific task, like calling an API to store or retrieve data. +You can use other Dapr APIs inside an activity. + +Activity definitions use the `activity` decorator to define an activity. +The first argument (`ctx`) is the `WorkflowActivityContext` and provides +the name of the activity and the workflow instance. +The second argument (`act_input`) is the input parameter for the activity. +There can only be one input parameter. Use a class if multiple input values are required. +""" @wf_runtime.activity(name='activity1') -def activity1(ctx: WorkflowActivityContext, input): - print(f'activity1: Received input: {input}.') - return f"{input} Two" +def activity1(ctx: WorkflowActivityContext, act_input: str) -> str: + print(f'activity1: Received input: {act_input}.') + return f"{act_input} Two" @wf_runtime.activity(name='activity2') -def activity2(ctx: WorkflowActivityContext, input): - print(f'activity2: Received input: {input}.') - return f"{input} Three" \ No newline at end of file +def activity2(ctx: WorkflowActivityContext, act_input: str) -> str: + print(f'activity2: Received input: {act_input}.') + return f"{act_input} Three" \ No newline at end of file