浙江省一建建设集团网站首页,免费网站打包app,网站出现的问题吗,app开发过程中的困难context 的作用
go 的编程中#xff0c;常常会在一个 goroutine 中启动多个 goroutine#xff0c;然后有可能在这些 goroutine 中又启动多个 goroutine。 如上图#xff0c;在 main 函数中#xff0c;启动了一个 goroutine A 和 goroutine B#xff0c;然后 goroutine A …context 的作用
go 的编程中常常会在一个 goroutine 中启动多个 goroutine然后有可能在这些 goroutine 中又启动多个 goroutine。 如上图在 main 函数中启动了一个 goroutine A 和 goroutine B然后 goroutine A 中又启动了 goroutine A1 和 goroutine A2goroutine B 中也是。
有时候我们可能想要取消当前的处理这个时候自然而然的也要取消子协程的执行进程。这个时候就需要一种机制来做这件事。context 就是设计来做这件事的。
比如在 web 应用中当子协程的某些处理时间过长的时候我们可能想要终止下游的处理防止协程长期占用资源。保证其他客户端的请求正常处理。
context 的不同类型
context.Background
往往用做父 context比如在 main 中定义的 context然后在 main 中将这个 context 传递给子协程。
context.TODO
不需要使用什么类型的 context 的时候使用这个 context.TODO
context.WithTimeout
我们需要对子 goroutine 做一些超时控制的时候使用这个 context比如超过多少秒就不再做处理。
context.WithDeadline
和 context.WithTimeout 类似只不过参数是一个 time.Time而不是 time.Duration
context.WithCancel
如果在父 goroutine 里面需要在某些情况下取消执行的时候可以使用这种 context。
实例
context.Background
package mainimport (contextfmttime
)func main() {ctx : context.Background()go func(ctx2 context.Context) {fmt.Println(ctx2)ctx3, cancel : context.WithCancel(ctx2)go func(ctx4 context.Context) {fmt.Println(ctx4)}(ctx3)cancel()}(ctx)time.Sleep(time.Millisecond * 5)
}在 main 入口里顶层的 context 使用 context.Background子 goroutine 里面可以针对实际情况基于父 context 派生新的 context比如加入如果需要对子 goroutine 做一些条件性的取消操作就可以像上面那样使用 ctx3, cancel : context.WithCancel(ctx2) 来基于父 context 创建一个新的 context然后我们可以通过 cancel 来给子 goroutine 发送取消信号。 注意这里的用语这里说发送取消信号因为事实上是否取消后续操作的控制权还是在子 goroutine 里面。但是子 goroutine 有义务停止当前 goroutine 的操作。 个人觉得一个原因是可能子 goroutine 里面有一些清理操作需要进行比如写个 Log 说当前操作被取消了这种情况下直接强行取消并不是很好的选择所以把控制权交给子 goroutine。 这一点可能在大多数文章里面可能没有提到但是笔者觉得如果明白了这一点的话对于理解 context 的工作机制很有帮助。
这种机制的感觉有点像是虽然你有权力不停止当前操作但是你有义务去停止当前的处理给你这种权力只是为了让你有点反应时间。
context.TODO
这个就跳过吧好像没什么好说的
context.WithTimeout
func ExampleContextWithTimeout() {// 10 毫秒超时时间// context.WithTimeout 也返回了一个 cancel 函数但是我们这里不需要所以忽略了。ctx, _ : context.WithTimeout(context.Background(), time.Millisecond * 10)var wg sync.WaitGroupwg.Add(1)go func(ctx context.Context) {defer wg.Done()// sleep 20 毫秒模拟耗时任务time.Sleep(time.Millisecond * 20)select {case -ctx.Done():// 因为已经超时了所以 ctx.Done() 返回的 channel 直接返回了因为已经关闭了// 我们可以使用 ctx.Err() 来查看具体的原因这里是 context deadline exceededfmt.Println(ctx.Err())returndefault:fmt.Println(in goroutine)}}(ctx)wg.Wait()// Output:// context deadline exceeded
}context.WithDeadline
func ExampleContextWithDeadline() {// 10 毫秒超时时间// context.WithDeadline 也返回了一个 cancel 函数但是我们这里不需要所以忽略了。ctx, _ : context.WithDeadline(context.Background(), time.Now().Add(time.Millisecond * 10))var wg sync.WaitGroupwg.Add(1)go func(ctx context.Context) {defer wg.Done()// sleep 20 毫秒模拟耗时任务time.Sleep(time.Millisecond * 20)select {case -ctx.Done():// 因为已经到达了 deadline所以 ctx.Done() 返回的 channel 直接返回了因为这个 channel 已经关闭了// ctx.Err() 同 context.WithTimeout也是 context deadline exceededfmt.Println(ctx.Err())returndefault:fmt.Println(in goroutine)}}(ctx)wg.Wait()// Output:// context deadline exceeded
}context.WithCancel
func ExampleContextWithCancel() {// context.WithCancel 返回的第二个值是一个可以调用的函数调用的时候子协程里面的 context 可以通过 ctx.Done() 获取到取消的信号ctx, cancel : context.WithCancel(context.Background())var wg sync.WaitGroupwg.Add(1)go func(ctx context.Context) {defer wg.Done()for {select {case -ctx.Done():fmt.Println(ctx.Err())returndefault:fmt.Println(ExampleContextWithCancel default)}}}(ctx)cancel()wg.Wait()// Output:// context canceled
}其实我们可以发现context.WithTimeout 和 context.WithDeadline 也返回了一个 cancelFunccontext.WithCancel 返回的 cancelFunc 和这个的效果一样。
只不过 context.WithTimeout 和 context.WithDeadline 多提供了一个时间上的控制。
总结 golang 中的 context 提供了一种父子 goroutine 之间沟通的机制 context.WithTimeout、context.WithDeadline、context.WithCancel 都返回一个新的 context 和一个 cancelFunccancelFunc 可以用来取消子 goroutine goroutine 最终是否停止取决于子 goroutine 本身但是我们有必要去监听 ctx.Done() 来根据父 goroutine 传递的信号来决定是否继续执行还是直接返回。