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

Function basicAsyncExample

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

basicAsyncExample 基础异步执行示例

(ctx context.Context)

Source from the content-addressed store, hash-verified

41
42// basicAsyncExample 基础异步执行示例
43func 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 进度跟踪示例
71func progressTrackingExample(ctx context.Context) {

Callers 1

mainFunction · 0.85

Calls 5

NewLongRunningExecutorFunction · 0.92
WaitForCompletionFunction · 0.92
PrintfMethod · 0.80
StartAsyncMethod · 0.65

Tested by

no test coverage detected