MCPcopy Create free account
hub / github.com/astercloud/aster / parallelExample

Function parallelExample

examples/workflow-agents/main.go:78–112  ·  view source on GitHub ↗

parallelExample 并行执行示例

(ctx context.Context)

Source from the content-addressed store, hash-verified

76
77// parallelExample 并行执行示例
78func parallelExample(ctx context.Context) {
79 // 创建多个算法 Agent
80 agents := []workflow.Agent{
81 NewMockAgent("AlgorithmA", "方案A:快速但粗糙"),
82 NewMockAgent("AlgorithmB", "方案B:慢但精确"),
83 NewMockAgent("AlgorithmC", "方案C:平衡"),
84 }
85
86 // 创建 ParallelAgent
87 parallel, err := workflow.NewParallelAgent(workflow.ParallelConfig{
88 Name: "MultiAlgorithm",
89 SubAgents: agents,
90 })
91 if err != nil {
92 log.Fatal(err)
93 }
94
95 // 执行
96 fmt.Println("开始并行执行:")
97 resultCount := 0
98 reader := parallel.Execute(ctx, "求解问题")
99 for {
100 event, err := reader.Recv()
101 if err != nil {
102 if errors.Is(err, io.EOF) {
103 break
104 }
105 log.Printf("错误: %v", err)
106 continue
107 }
108 resultCount++
109 printEvent(event)
110 }
111 fmt.Printf("收到 %d 个并行结果\n", resultCount)
112}
113
114// loopExample 循环优化示例
115func loopExample(ctx context.Context) {

Callers 1

mainFunction · 0.85

Calls 6

ExecuteMethod · 0.95
NewParallelAgentFunction · 0.92
NewMockAgentFunction · 0.85
printEventFunction · 0.85
RecvMethod · 0.80
PrintfMethod · 0.80

Tested by

no test coverage detected