ConcurrentCron is a task scheduler that supports high concurrency at the same time which lets you run Go functions periodically at pre-determined interval using a simple, human-friendly syntax.
You can run this scheduler in the following way
func test(num int) {
fmt.Println("before:im a task", num)
time.Sleep(10 * time.Second)
fmt.Println("after:im a task", num, time.Now())
}
func main() {
scheduler, err := ConcurrencyCron.NewScheduler(200) //200 is the number of tasks that can be run in parallel
if err != nil {
fmt.Println(err)
}
for i := 0; i < 200; i++ {
scheduler.Every(1).Seconds().Do(test, i)
scheduler.Every(1).Minutes().Do(test, 1000+i)
scheduler.Every(1).Hours().Do(test, 10000+i)
}
ctx, cancel := context.WithCancel(context.Background())
scheduler.Start(ctx)
ch := make(chan bool)
<-ch //test
scheduler.Stop(cancel) //stop the tasks
}
This article refers to some of jasonlvhit/gocron's ideas and things, the specific timing tasks are the same as gocron, you can refer to his project
Thank you for the support and understanding ,jasonlvhit!
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传