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

Function demonstrateMultipleTasks

examples/scheduler/main.go:119–156  ·  view source on GitHub ↗

示例 4: 多任务调度

()

Source from the content-addressed store, hash-verified

117
118// 示例 4: 多任务调度
119func demonstrateMultipleTasks() {
120 fmt.Println("--- 示例 4: 多任务调度 ---")
121
122 scheduler := core.NewScheduler(nil)
123 defer scheduler.Shutdown()
124
125 // 任务 1: 每 2 步
126 _, _ = scheduler.EverySteps(2, func(ctx context.Context, stepCount int) error {
127 fmt.Printf(" [任务A] 每2步 - 第 %d 步\n", stepCount)
128 return nil
129 })
130
131 // 任务 2: 每 5 步
132 _, _ = scheduler.EverySteps(5, func(ctx context.Context, stepCount int) error {
133 fmt.Printf(" [任务B] 每5步 - 第 %d 步\n", stepCount)
134 return nil
135 })
136
137 // 任务 3: 每 300ms
138 _, _ = scheduler.EveryInterval(300*time.Millisecond, func(ctx context.Context) error {
139 fmt.Printf(" [任务C] 定时触发\n")
140 return nil
141 })
142
143 fmt.Printf("已创建 3 个任务\n")
144 fmt.Printf("步骤任务: %d, 定时任务: %d\n",
145 scheduler.GetStepTaskCount(),
146 scheduler.GetIntervalTaskCount())
147
148 // 模拟步骤
149 for i := 1; i <= 10; i++ {
150 scheduler.NotifyStep(i)
151 time.Sleep(150 * time.Millisecond)
152 }
153
154 time.Sleep(200 * time.Millisecond)
155 fmt.Println()
156}
157
158// 示例 5: 任务取消
159func demonstrateTaskCancellation() {

Callers 1

mainFunction · 0.85

Calls 8

ShutdownMethod · 0.95
EveryStepsMethod · 0.95
EveryIntervalMethod · 0.95
GetStepTaskCountMethod · 0.95
GetIntervalTaskCountMethod · 0.95
NotifyStepMethod · 0.95
NewSchedulerFunction · 0.92
PrintfMethod · 0.80

Tested by

no test coverage detected