basicAsyncExample 基础异步执行示例
(ctx context.Context)
| 41 | |
| 42 | // basicAsyncExample 基础异步执行示例 |
| 43 | func basicAsyncExample(ctx context.Context) { |
| 44 | // 1. 创建执行器 |
| 45 | executor := tools.NewLongRunningExecutor() |
| 46 | |
| 47 | // 2. 创建模拟的长时运行工具 |
| 48 | tool := NewMockDataProcessingTool(executor) |
| 49 | |
| 50 | // 3. 启动异步任务 |
| 51 | taskID, err := tool.StartAsync(ctx, map[string]any{ |
| 52 | "data_size": 1000, |
| 53 | "delay_ms": 500, |
| 54 | }) |
| 55 | if err != nil { |
| 56 | log.Fatalf("Failed to start task: %v", err) |
| 57 | } |
| 58 | |
| 59 | fmt.Printf("✅ Task started: %s\n", taskID) |
| 60 | |
| 61 | // 4. 等待任务完成 |
| 62 | status, err := tools.WaitForCompletion(executor, taskID, 100*time.Millisecond, 5*time.Second) |
| 63 | if err != nil { |
| 64 | log.Fatalf("Failed to wait for task: %v", err) |
| 65 | } |
| 66 | |
| 67 | fmt.Printf("✅ Task completed: state=%s, result=%v\n", status.State, status.Result) |
| 68 | } |
| 69 | |
| 70 | // progressTrackingExample 进度跟踪示例 |
| 71 | func progressTrackingExample(ctx context.Context) { |
no test coverage detected