Go语言同步和异步执行多个任务封装
同步适合多个连续执行的,每一步的执行依赖于上一步操作,异步执行则和任务执行顺序无关(如从10个站点抓取数据)
同步执行类RunnerAsync
支持返回超时检测,系统中断检测
错误常量定义,task/err.go
1 2 3 4 5 6 7 8 9 10 | package task import "errors" //超时错误 var ErrTimeout = errors.New( "received timeout" ) //操作系统系统中断错误 var ErrInterrupt = errors.New( "received interrupt" ) |
实现代码如下,task/runner_async.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | package task import ( "os" "os/signal" "time" ) //同步执行任务 type RunnerAsync struct { //操作系统的信号检测 interrupt chan os.Signal //记录执行完成的状态 complete chan error //超时检测 timeout <- chan time.Time //保存所有要执行的任务,顺序执行 tasks [] func (id int) } //new一个RunnerAsync对象 func NewRunnerAsync(d time.Duration) *RunnerAsync { return &RunnerAsync{ interrupt: make( chan os.Signal, 1), complete: make( chan error), timeout: time.After(d), } } //添加一个任务 func (this *RunnerAsync) Add(tasks ... func (id int)) { this.tasks = append(this.tasks, tasks...) } //启动RunnerAsync,监听错误信息 func (this *RunnerAsync) Start() error { //接收操作系统信号 signal.Notify(this.interrupt, os.Interrupt) //执行任务 go func () { this.complete <- this.Run() }() select { //返回执行结果 case err := <-this.complete: return err //超时返回 case <-this.timeout: return ErrTimeout } } //顺序执行所有的任务 func (this *RunnerAsync) Run() error { for id, task := range this.tasks { if this.gotInterrupt() { return ErrInterrupt } //执行任务 task(id) } return nil } //判断是否接收到操作系统中断信号 func (this *RunnerAsync) gotInterrupt() bool { select { case <-this.interrupt: //停止接收别的信号 signal.Stop(this.interrupt) return true //正常执行 default : return false } } |
使用方法
Add添加一个任务,任务为接收int类型的一个闭包
Start开始执行伤,返回一个error类型,nil为执行完毕, ErrTimeout代表执行超时,ErrInterrupt代表执行被中断(类似Ctrl + C操作)
测试代码
task/runner_async_test.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | package task import ( "fmt" "os" "runtime" "testing" "time" ) func TestRunnerAsync_Start(t *testing.T) { //开启多核 runtime.GOMAXPROCS(runtime.NumCPU()) //创建runner对象,设置超时时间 runner := NewRunnerAsync(8 * time.Second) //添加运行的任务 runner.Add( createTaskAsync(), createTaskAsync(), createTaskAsync(), createTaskAsync(), createTaskAsync(), createTaskAsync(), createTaskAsync(), createTaskAsync(), createTaskAsync(), createTaskAsync(), createTaskAsync(), createTaskAsync(), createTaskAsync(), ) fmt.Println( "同步执行任务" ) //开始执行任务 if err := runner.Start(); err != nil { switch err { case ErrTimeout: fmt.Println( "执行超时" ) os.Exit(1) case ErrInterrupt: fmt.Println( "任务被中断" ) os.Exit(2) } } t.Log( "执行结束" ) } //创建要执行的任务 func createTaskAsync() func (id int) { return func (id int) { fmt.Printf( "正在执行%v个任务\n" , id) //模拟任务执行,sleep两秒 //time.Sleep(1 * time.Second) } } |
执行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 同步执行任务 正在执行0个任务 正在执行1个任务 正在执行2个任务 正在执行3个任务 正在执行4个任务 正在执行5个任务 正在执行6个任务 正在执行7个任务 正在执行8个任务 正在执行9个任务 正在执行10个任务 正在执行11个任务 正在执行12个任务 |
异步执行类Runner
支持返回超时检测,系统中断检测
实现代码如下,task/runner.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | package task import ( "os" "time" "os/signal" "sync" ) //异步执行任务 type Runner struct { //操作系统的信号检测 interrupt chan os.Signal //记录执行完成的状态 complete chan error //超时检测 timeout <- chan time.Time //保存所有要执行的任务,顺序执行 tasks [] func (id int) error waitGroup sync.WaitGroup lock sync.Mutex errs []error } //new一个Runner对象 func NewRunner(d time.Duration) *Runner { return &Runner{ interrupt: make( chan os.Signal, 1), complete: make( chan error), timeout: time.After(d), waitGroup: sync.WaitGroup{}, lock: sync.Mutex{}, } } //添加一个任务 func (this *Runner) Add(tasks ... func (id int) error) { this.tasks = append(this.tasks, tasks...) } //启动Runner,监听错误信息 func (this *Runner) Start() error { //接收操作系统信号 signal.Notify(this.interrupt, os.Interrupt) //并发执行任务 go func () { this.complete <- this.Run() }() select { //返回执行结果 case err := <-this.complete: return err //超时返回 case <-this.timeout: return ErrTimeout } } //异步执行所有的任务 func (this *Runner) Run() error { for id, task := range this.tasks { if this.gotInterrupt() { return ErrInterrupt } this.waitGroup.Add(1) go func (id int) { this.lock.Lock() //执行任务 err := task(id) //加锁保存到结果集中 this.errs = append(this.errs, err) this.lock.Unlock() this.waitGroup.Done() }(id) } this.waitGroup.Wait() return nil } //判断是否接收到操作系统中断信号 func (this *Runner) gotInterrupt() bool { select { case <-this.interrupt: //停止接收别的信号 signal.Stop(this.interrupt) return true //正常执行 default : return false } } //获取执行完的error func (this *Runner) GetErrs() []error { return this.errs } |
使用方法
Add添加一个任务,任务为接收int类型,返回类型error的一个闭包
Start开始执行伤,返回一个error类型,nil为执行完毕, ErrTimeout代表执行超时,ErrInterrupt代表执行被中断(类似Ctrl + C操作)
getErrs获取所有的任务执行结果
测试示例代码
task/runner_test.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package task import ( "testing" "time" "fmt" "os" "runtime" ) func TestRunner_Start(t *testing.T) { //开启多核心 runtime.GOMAXPROCS(runtime.NumCPU()) //创建runner对象,设置超时时间 runner := NewRunner(18 * time.Second) //添加运行的任务 runner.Add( createTask(), createTask(), createTask(), createTask(), createTask(), createTask(), createTask(), createTask(), createTask(), createTask(), createTask(), createTask(), createTask(), createTask(), ) fmt.Println( "异步执行任务" ) //开始执行任务 if err := runner.Start(); err != nil { switch err { case ErrTimeout: fmt.Println( "执行超时" ) os.Exit(1) case ErrInterrupt: fmt.Println( "任务被中断" ) os.Exit(2) } } t.Log( "执行结束" ) t.Log(runner.GetErrs()) } //创建要执行的任务 func createTask() func (id int) error { return func (id int) error { fmt.Printf( "正在执行%v个任务\n" , id) //模拟任务执行,sleep //time.Sleep(1 * time.Second) return nil } } |
执行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 异步执行任务 正在执行2个任务 正在执行1个任务 正在执行4个任务 正在执行3个任务 正在执行6个任务 正在执行5个任务 正在执行9个任务 正在执行7个任务 正在执行10个任务 正在执行13个任务 正在执行8个任务 正在执行11个任务 正在执行12个任务 正在执行0个任务 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)