1 Star 0 Fork 0

丁杨庄/dgoflow

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
parallel.go 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
13950056074 提交于 2024-08-15 14:58 +08:00 . 修改
package parallel
import (
"fmt"
flow "gitee.com/dingyangzhuang/dgoflow/flow/v1"
"time"
)
// Workload function
func node1(data []byte, option map[string][]string) ([]byte, error) {
result := fmt.Sprintf("(Executing node 1 with data (%s))", string(data))
time.Sleep(time.Second * 5)
fmt.Println(result)
return []byte(result), nil
}
// Workload function
func node2(data []byte, option map[string][]string) ([]byte, error) {
result := fmt.Sprintf("(Executing node 2 with data (%s))", string(data))
time.Sleep(time.Second * 5)
fmt.Println(result)
return []byte(result), nil
}
// Workload function
func node3(data []byte, option map[string][]string) ([]byte, error) {
result := fmt.Sprintf("(Executing node 3 with data (%s))", string(data))
time.Sleep(time.Second * 5)
fmt.Println(result)
return []byte(result), nil
}
// Workload function
func node4(data []byte, option map[string][]string) ([]byte, error) {
result := fmt.Sprintf("(Executing node 4 with data (%s))", string(data))
time.Sleep(time.Second * 5)
fmt.Println(result)
return []byte(result), nil
}
// Aggregator can be used to collect and map response from multiple in-degree as a request to node
// here node4 uses the result from node2 and node3
func node4Aggregator(data map[string][]byte) ([]byte, error) {
aggregatedResult := fmt.Sprintf("(node2: %s, node3: %s)", string(data["node2"]), string(data["node3"]))
return []byte(aggregatedResult), nil
}
// DefineWorkflow Define provide definition of the workflow
func DefineWorkflow(workflow *flow.Workflow, context *flow.Context) error {
dag := workflow.Dag()
dag.Node("node1", node1)
dag.Node("node2", node2)
dag.Node("node3", node3)
dag.Node("node4", node4, flow.Aggregator(node4Aggregator))
dag.Edge("node1", "node2")
dag.Edge("node1", "node3")
dag.Edge("node3", "node4")
dag.Edge("node2", "node4")
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dingyangzhuang/dgoflow.git
git@gitee.com:dingyangzhuang/dgoflow.git
dingyangzhuang
dgoflow
dgoflow
v0.0.5

搜索帮助