This directory contains examples of using the Dapr Workflow extension. You can find additional information about these examples in the Dapr Workflow Application Patterns docs.
You can install dapr SDK package using pip command:
pip3 install -r requirements.txtEach of the examples in this directory can be run directly from the command line.
This example represents a workflow that manages counters through a series of activities and child workflows. It shows several Dapr Workflow features including:
- Basic activity execution with counter increments
- Retryable activities with configurable retry policies
- Child workflow orchestration with retry logic
- External event handling with timeouts
- Workflow state management (pause/resume)
- Activity error handling and retry backoff
- Global state tracking across workflow components
- Workflow lifecycle management (start, pause, resume, purge)
dapr run --app-id wf-simple-example --dapr-grpc-port 50001 -- python3 simple.pyThe output of this example should look like this:
- "== APP == Hi Counter!"
- "== APP == New counter value is: 1!"
- "== APP == New counter value is: 11!"
- "== APP == Retry count value is: 0!"
- "== APP == Retry count value is: 1! This print statement verifies retry"
- "== APP == Appending 1 to child_orchestrator_string!"
- "== APP == Appending a to child_orchestrator_string!"
- "== APP == Appending a to child_orchestrator_string!"
- "== APP == Appending 2 to child_orchestrator_string!"
- "== APP == Appending b to child_orchestrator_string!"
- "== APP == Appending b to child_orchestrator_string!"
- "== APP == Appending 3 to child_orchestrator_string!"
- "== APP == Appending c to child_orchestrator_string!"
- "== APP == Appending c to child_orchestrator_string!"
- "== APP == Get response from hello_world_wf after pause call: SUSPENDED"
- "== APP == Get response from hello_world_wf after resume call: RUNNING"
- "== APP == New counter value is: 111!"
- "== APP == New counter value is: 1111!"
- "== APP == Workflow completed! Result: Completed"
This example demonstrates how to chain "activity" tasks together in a workflow. You can run this sample using the following command:
dapr run --app-id wfexample --dapr-grpc-port 50001 -- python3 task_chaining.pyThe output of this example should look like this:
== APP == Workflow started. Instance ID: b716208586c24829806b44b62816b598
== APP == Step 1: Received input: 42.
== APP == Step 2: Received input: 43.
== APP == Step 3: Received input: 86.
== APP == Workflow completed! Status: WorkflowStatus.COMPLETED
This example demonstrates how to fan-out a workflow into multiple parallel tasks, and then fan-in the results of those tasks. You can run this sample using the following command:
dapr run --app-id wfexample --dapr-grpc-port 50001 -- python3 fan_out_fan_in.pyThe output of this sample should look like this:
== APP == Workflow started. Instance ID: 2e656befbb304e758776e30642b75944
== APP == Processing work item: 1.
== APP == Processing work item: 2.
== APP == Processing work item: 3.
== APP == Processing work item: 4.
== APP == Processing work item: 5.
== APP == Processing work item: 6.
== APP == Processing work item: 7.
== APP == Processing work item: 8.
== APP == Processing work item: 9.
== APP == Processing work item: 10.
== APP == Work item 1 processed. Result: 2.
== APP == Work item 2 processed. Result: 4.
== APP == Work item 3 processed. Result: 6.
== APP == Work item 4 processed. Result: 8.
== APP == Work item 5 processed. Result: 10.
== APP == Work item 6 processed. Result: 12.
== APP == Work item 7 processed. Result: 14.
== APP == Work item 8 processed. Result: 16.
== APP == Work item 9 processed. Result: 18.
== APP == Work item 10 processed. Result: 20.
== APP == Final result: 110.
Note that the ordering of the work-items is non-deterministic since they are all running in parallel.
This example demonstrates how to use a workflow to interact with a human user. This example requires input from the user, so you'll need to have a separate command for the Dapr CLI and the Python app.
The Dapr CLI can be started using the following command:
dapr run --app-id wfexample --dapr-grpc-port 50001In a separate terminal window, run the following command to start the Python workflow app:
python3 human_approval.pyWhen you run the example, you will see output as well as a prompt like this:
*** Requesting approval from user for order: namespace(cost=2000, product='MyProduct', quantity=1)
Press [ENTER] to approve the order...
Press the ENTER key to continue the workflow. If ENTER is pressed before the hardcoded timeout expires, then the following output will be displayed:
*** Placing order: namespace(cost=2000, product='MyProduct', quantity=1)
Workflow completed! Result: "Approved by 'Me'"
However, if the timeout expires before ENTER is pressed, then the following output will be displayed:
*** Workflow timed out!
This example demonstrates how to eternally running workflow that polls an endpoint to detect service health events. This example requires input from the user, so you'll need to have a separate command for the Dapr CLI and the Python app.
The Dapr CLI can be started using the following command:
dapr run --app-id wfexample --dapr-grpc-port 50001In a separate terminal window, run the following command to start the Python workflow app:
python3 monitor.pyThe workflow runs forever, or until the app is stopped. While it is running, it will periodically report information about whether a "job" is healthy or unhealthy. After several minutes, the output of this workflow will look something like this (note that the healthy and unhealthy message ordering is completely random):
Press Enter to stop...
Job 'job1' is healthy.
Job 'job1' is healthy.
Job 'job1' is unhealthy.
*** Alert: Job 'job1' is unhealthy!
Job 'job1' is healthy.
Job 'job1' is healthy.
Job 'job1' is healthy.
Job 'job1' is unhealthy.
*** Alert: Job 'job1' is unhealthy!
Job 'job1' is unhealthy.
This workflow runs forever or until you press ENTER to stop it. Starting the app again after stopping it will cause the same workflow instance to resume where it left off.
This example demonstrates how to call a child workflow. The Dapr CLI can be started using the following command:
dapr run --app-id wfexample --dapr-grpc-port 50001In a separate terminal window, run the following command to start the Python workflow app:
python3 child_workflow.pyWhen you run the example, you will see output like this:
...
*** Calling child workflow 29a7592a1e874b07aad2bb58de309a51-child
*** Child workflow 6feadc5370184b4998e50875b20084f6 called
...