context通过发送取消信号控制goroutine生命周期,不能直接终止,需goroutine主动退出。2. 使用WithCancel或WithTimeout创建可取消的context,通过Done() channel通知取消。3. 必须调用cancel函数释放资源,避免泄漏。4. 多层goroutine需传递context实现级联取消。5. 结合select监听取消、定时器等多事件。

在Go语言中,context 是控制goroutine生命周期的核心机制。当你需要取消正在运行的goroutine,比如用户请求中断、超时或程序关闭时,context提供了标准方式来传递取消信号。下面介绍几种常见且实用的使用模式。
Context本身不能直接“杀死”goroutine,它通过发送通知让目标goroutine主动退出。每个context可以携带一个只读的Done() channel,当该channel被关闭时,表示上下文已被取消。
关键点:
这是最基础的场景,主程序启动goroutine后,在某个条件满足时主动取消。
立即学习“go语言免费学习笔记(深入)”;
func main() { ctx, cancel := context.WithCancel(context.Background()) defer cancel()go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("goroutine退出:", ctx.Err())
return
default:
fmt.Println("工作中...")
time.Sleep(500 * time.Millisecond)
}
}
}(ctx)
time.Sleep(2 * time.Second)
cancel() // 触发取消
time.Sleep(1 * time.Second) // 等待退出 }
很多场景不需要手动cancel,而是设定超时时间自动终止任务。
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel()go func() {
timer := time.NewTimer(5 * time.Second)
select {
case <-timer.C:
fmt.Println("任务完成")
case <-ctx.Done():
fmt.Println("任务被取消:", ctx.Err())
if !timer.Stop() {
<-timer.C // 防止泄漏
}
}
}()
3秒后ctx.Done()触发,即使任务未完成也会退出。注意处理timer避免资源泄漏。
当一个goroutine又启动了子goroutine时,必须将context继续传递下去,确保整条调用链都能响应取消。
go func(parentCtx context.Context) { childCtx, childCancel := context.WithCancel(parentCtx) defer childCancel()go func() {
for {
select {
case <-childCtx.Done():
fmt.Println("子goroutine退出")
return
default:
time.Sleep(300 * time.Millisecond)
}
}
}()
// 模拟工作
time.Sleep(2 * time.Second) }(ctx)
父级context取消时,childCtx也会被触发,实现级联退出。
实际开发中,goroutine往往要同时监听取消、数据到达、定时器等多种事件。
for { select { case只要任意一个case触发就会跳出循环,其中ctx.Done()优先级最高。
基本上就这些。掌握context取消的核心在于:传递context、监听Done channel、及时调用cancel。只要遵循这些原则,就能写出可控制的并发程序。
以上就是如何在Golang中使用context取消goroutine_Golang context取消goroutine实践汇总的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号