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

Function cancellationExample

examples/long-running-tools/main.go:106–132  ·  view source on GitHub ↗

cancellationExample 任务取消示例

(ctx context.Context)

Source from the content-addressed store, hash-verified

104
105// cancellationExample 任务取消示例
106func cancellationExample(ctx context.Context) {
107 executor := tools.NewLongRunningExecutor()
108 tool := NewMockDataProcessingTool(executor)
109
110 // 启动一个耗时任务
111 taskID, _ := tool.StartAsync(ctx, map[string]any{
112 "data_size": 10000,
113 "delay_ms": 3000, // 3秒
114 })
115
116 fmt.Printf("✅ Task started: %s\n", taskID)
117
118 // 等待一会儿
119 time.Sleep(500 * time.Millisecond)
120
121 // 取消任务
122 if err := executor.Cancel(ctx, taskID); err != nil {
123 log.Fatalf("Failed to cancel task: %v", err)
124 }
125
126 fmt.Println("✅ Cancel signal sent")
127
128 // 验证任务已取消
129 time.Sleep(200 * time.Millisecond)
130 status, _ := executor.GetStatus(ctx, taskID)
131 fmt.Printf("✅ Task state: %s\n", status.State)
132}
133
134// concurrentTasksExample 并发多任务示例
135func concurrentTasksExample(ctx context.Context) {

Callers 1

mainFunction · 0.85

Calls 6

CancelMethod · 0.95
GetStatusMethod · 0.95
NewLongRunningExecutorFunction · 0.92
PrintfMethod · 0.80
StartAsyncMethod · 0.65

Tested by

no test coverage detected