go - 上下文包与完成通道以避免 goroutine 泄漏
问题描述
清理 goroutine 有两种不同的方法。
使用 kill 通道表示取消,使用 done 通道表示 goroutine 已终止。
type Worker struct { Done chan struct{} Kill chan struct{} Jobs chan Job } func (w *Worker) Run() { defer func() { w.Done <- struct{}{} } for { select { case <-w.Kill: return case j := <-w.Jobs: // Do some work } } go w.Run() w.Kill <- struct{}{}
用于
context
取消type Worker struct { Ctx context.Context Cancel context.CancelFunc Jobs chan Job } func (w *Worker) Run() { for { select { case <-w.Ctx.Done(): return case j := <-w.Jobs: // Do some work } } go w.Run() w.Cancel()
每种方法的优缺点是什么?我应该默认哪个?
我知道如果我想杀死一棵相互连接的 goroutines 树,我应该使用上下文方法,但假设我有一个简单的 worker,它不会在内部启动其他 goroutines。
解决方案
Go 1.7 将 golang.org/x/net/context 包作为上下文移动到标准库中。这允许在其他标准库包中使用上下文来取消、超时和传递请求范围的数据,包括 net、net/http 和 os/exec,如下所述。
有关上下文的更多信息,请参阅包文档和 Go 博客文章“<a href="https://blog.golang.org/context" rel="nofollow noreferrer">Go 并发模式:上下文。”</p >
有问题。引入了上下文包来解决它们。
现在您已经阅读了所有相关文档,您的问题是什么?
推荐阅读
- html - 如何通过输入标签显示数据
- python - 使用换行符和自然语言在熊猫中读取 csv 文件
- python-3.x - Python __getattribute__ 与 'is-a'/'has-a' 关系
- php - 使用 PHP 和 OpenSSL 生成对称密钥 AES-256-CBC
- javascript - 如何避免涉及 Node.js、Redis 和 MongoDB 的潜在竞争条件
- operating-system - 如何以编程方式计算 Ubuntu 中的中断延迟?
- sas - 卡在sas代码上。第一次在 SAS 中执行 R 代码
- java - 获取 org.hibernate.MappingException:没有 JDBC 类型的方言映射:-8 异常
- rabbitmq - Rabbitmq Prometheus Exporter vs Prometheus Plugin for RabbitMQ
- reactjs - 我收到堆栈跟踪错误,无法解决