Skip to content

Commit 3dd98dc

Browse files
Move workflows examples, based on review
Signed-off-by: Elena Kolevska <[email protected]>
1 parent 8dad563 commit 3dd98dc

File tree

2 files changed

+69
-68
lines changed

2 files changed

+69
-68
lines changed

daprdocs/content/en/python-sdk-docs/python-client.md

-67
Original file line numberDiff line numberDiff line change
@@ -167,73 +167,6 @@ The base endpoint for gRPC calls is the one used for the client initialisation (
167167
- For a full guide on service invocation visit [How-To: Invoke a service]({{< ref howto-invoke-discover-services.md >}}).
168168
- Visit [Python SDK examples](https://github.com/dapr/python-sdk/tree/master/examples/invoke-simple) for code samples and instructions to try out service invocation.
169169

170-
### Workflow
171-
172-
```python
173-
from time import sleep
174-
175-
import dapr.ext.workflow as wf
176-
177-
178-
wfr = wf.WorkflowRuntime()
179-
180-
181-
@wfr.workflow(name='random_workflow')
182-
def task_chain_workflow(ctx: wf.DaprWorkflowContext, wf_input: int):
183-
try:
184-
result1 = yield ctx.call_activity(step1, input=wf_input)
185-
result2 = yield ctx.call_activity(step2, input=result1)
186-
except Exception as e:
187-
yield ctx.call_activity(error_handler, input=str(e))
188-
raise
189-
# TODO update to set custom status
190-
return [result1, result2]
191-
192-
193-
@wfr.activity(name='step1')
194-
def step1(ctx, activity_input):
195-
print(f'Step 1: Received input: {activity_input}.')
196-
# Do some work
197-
return activity_input + 1
198-
199-
200-
@wfr.activity
201-
def step2(ctx, activity_input):
202-
print(f'Step 2: Received input: {activity_input}.')
203-
# Do some work
204-
return activity_input * 2
205-
206-
@wfr.activity
207-
def error_handler(ctx, error):
208-
print(f'Executing error handler: {error}.')
209-
# Do some compensating work
210-
211-
212-
if __name__ == '__main__':
213-
wfr.start()
214-
sleep(10) # wait for workflow runtime to start
215-
216-
wf_client = wf.DaprWorkflowClient()
217-
instance_id = wf_client.schedule_new_workflow(workflow=task_chain_workflow, input=42)
218-
print(f'Workflow started. Instance ID: {instance_id}')
219-
state = wf_client.wait_for_workflow_completion(instance_id)
220-
print(f'Workflow completed! Status: {state.runtime_status}')
221-
222-
wfr.shutdown()
223-
```
224-
225-
- Learn more about authoring and managing workflows:
226-
- [How-To: Author a workflow]({{< ref howto-author-workflow.md >}}).
227-
- [How-To: Manage a workflow]({{< ref howto-manage-workflow.md >}}).
228-
- Visit [Python SDK examples](https://github.com/dapr/python-sdk/tree/main/examples/workflow) for code samples and instructions to try out Dapr Workflow:
229-
- [Task chaining example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/task_chaining.py)
230-
- [Fan-out/Fan-in example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/fan_out_fan_in.py)
231-
- [Child workflow example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/child_workflow.py)
232-
- [Human approval example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/human_approval.py)
233-
- [Monitor example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/monitor.py)
234-
235-
236-
237170
### Save & get application state
238171

239172
```python

daprdocs/content/en/python-sdk-docs/python-sdk-extensions/python-workflow-ext/_index.md

+69-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,80 @@ The development package will contain features and behavior that will be compatib
2727
{{% /alert %}}
2828

2929
```bash
30-
pip3 install dapr-ext-workflow-dev
30+
pip install dapr-ext-workflow-dev
3131
```
3232
{{% /codetab %}}
3333

3434
{{< /tabs >}}
3535

36+
## Example
37+
38+
```python
39+
from time import sleep
40+
41+
import dapr.ext.workflow as wf
42+
43+
44+
wfr = wf.WorkflowRuntime()
45+
46+
47+
@wfr.workflow(name='random_workflow')
48+
def task_chain_workflow(ctx: wf.DaprWorkflowContext, wf_input: int):
49+
try:
50+
result1 = yield ctx.call_activity(step1, input=wf_input)
51+
result2 = yield ctx.call_activity(step2, input=result1)
52+
except Exception as e:
53+
yield ctx.call_activity(error_handler, input=str(e))
54+
raise
55+
# TODO update to set custom status
56+
return [result1, result2]
57+
58+
59+
@wfr.activity(name='step1')
60+
def step1(ctx, activity_input):
61+
print(f'Step 1: Received input: {activity_input}.')
62+
# Do some work
63+
return activity_input + 1
64+
65+
66+
@wfr.activity
67+
def step2(ctx, activity_input):
68+
print(f'Step 2: Received input: {activity_input}.')
69+
# Do some work
70+
return activity_input * 2
71+
72+
@wfr.activity
73+
def error_handler(ctx, error):
74+
print(f'Executing error handler: {error}.')
75+
# Do some compensating work
76+
77+
78+
if __name__ == '__main__':
79+
wfr.start()
80+
sleep(10) # wait for workflow runtime to start
81+
82+
wf_client = wf.DaprWorkflowClient()
83+
instance_id = wf_client.schedule_new_workflow(workflow=task_chain_workflow, input=42)
84+
print(f'Workflow started. Instance ID: {instance_id}')
85+
state = wf_client.wait_for_workflow_completion(instance_id)
86+
print(f'Workflow completed! Status: {state.runtime_status}')
87+
88+
wfr.shutdown()
89+
```
90+
91+
- Learn more about authoring and managing workflows:
92+
- [How-To: Author a workflow]({{< ref howto-author-workflow.md >}}).
93+
- [How-To: Manage a workflow]({{< ref howto-manage-workflow.md >}}).
94+
-
95+
- Visit [Python SDK examples](https://github.com/dapr/python-sdk/tree/main/examples/workflow) for code samples and instructions to try out Dapr Workflow:
96+
- [Simple workflow example]({{< ref python-workflow.md >}})
97+
- [Task chaining example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/task_chaining.py)
98+
- [Fan-out/Fan-in example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/fan_out_fan_in.py)
99+
- [Child workflow example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/child_workflow.py)
100+
- [Human approval example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/human_approval.py)
101+
- [Monitor example](https://github.com/dapr/python-sdk/blob/main/examples/workflow/monitor.py)
102+
103+
36104
## Next steps
37105

38106
{{< button text="Getting started with the Dapr Workflow Python SDK" page="python-workflow.md" >}}

0 commit comments

Comments
 (0)