Golang语言中Context的使用方法

发布时间:2021-07-05 16:54:56 作者:chen
来源:亿速云 阅读:202

本篇内容介绍了“Golang语言中Context的使用方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

01、介绍

在 Golang 语言并发编程中,经常会遇到监控 goroutine 运行结束的场景,通常我们会想到使用 WaitGroup 和 chan +  select,其中 WaitGroup 用于监控一组 goroutine 是否全部运行结束,chan + select 用于监控一个 goroutine  是否运行结束(取消一个 goroutine)。

如果我们需要监控多个 goroutine 是否运行结束(取消多个 goroutine),通常会使用 context,当然使用 context  也可以用于监控一个 goroutine 是否运行结束(取消一个 goroutine)。我们在之前的文章已经介绍过 Golang 语言标准库  Context,未阅读的读者朋友们可以按需翻阅。本文我们主要介绍 Context 的一些使用方式。

02、取消一个 goroutine

使用 context 取消一个 goroutine,比较类似于使用 chan + select 的方式取消一个 goroutine。

示例代码:

func main () {  ctx, cancel := context.WithCancel(context.Background())  go func(ctx context.Context) {   for {    select {     case <-ctx.Done():      fmt.Println("goroutine 已停止")      return    default:     fmt.Println("goroutine 正在运行")     time.Sleep(time.Second)    }   }  }(ctx)  time.Sleep(time.Second * 5)  cancel()  time.Sleep(time.Second * 5)  fmt.Println("main goroutine 已结束") }

阅读上面这段代码,我们首先使用 context.Background() 创建一个 context 树的根节点,然后使用  context.WithCancel() 创建一个可取消的子 context 类型的变量 ctx,作为参数传递给子 goroutine,用作跟踪子  goroutine。

然后在子 goroutine 中,使用 for select 监控 <-ctx.Done() 判断子 goroutine 是否运行结束。

最后使用 context.WithCancel() 返回的第二个值 CancelFunc 类型的 cancel 变量给子 goroutine  发送取消指令。

03、取消多个 goroutine

接下来,我们再来看一个使用 context 停止多个 goroutine 的示例。

func main () {  ctx, cancel := context.WithCancel(context.Background())   // 停止多个 goroutine  go worker(ctx, "节点一")  go worker(ctx, "节点二")  go worker(ctx, "节点三")  time.Sleep(time.Second * 5)  cancel()  time.Sleep(time.Second * 5)  fmt.Println("main goroutine 已结束") }  func worker (ctx context.Context, node string) {  for {   select {    case <-ctx.Done():     fmt.Println(node, "goroutine 已停止")     return   default:    fmt.Println(node, "goroutine 正在运行")    time.Sleep(time.Second)   }  } }

阅读上面这段代码,我们使用 go 关键字启动三个 worker goroutine,和上个示例一样,首先创建一个 context  树的根节点,使用第一个返回值 context 类型的子 ctx 跟踪每一个 worker goroutine,在 worker 中使用 for seclect  监控 <-ctx.Done() 判断子 goroutine 是否运行结束,最后通过调用第二个返回值 CancelFunc 类型的 cancel 给子  goroutine 发送取消指令,此时所有子 context 都会接收到取消指令,goroutine 结束运行。

04、上下文信息传递

我们在前面的示例中使用 WithCancel 函数,用作取消 context,除此之外,可用作取消 Context 的函数还有 WithDeadline  函数和 WithTimeout 函数,分别用于定时取消和超时取消,限于篇幅,本文不再赘述,感兴趣的读者可以查阅官方标准库文档。除了上述三个函数外,还有一个  WithValue 函数,它是用作上下文信息传递的一个函数。

在 Golang 语言中,Context 包还有一个重要的作用,就是用作上下文信息传递,接下来我们介绍一下如何使用 WithValue  函数传递上下文信息。

示例代码:

func main () {  ctx, cancel := context.WithCancel(context.Background())  // 传递上下文信息  ctxValue := context.WithValue(ctx, "uid", 1)  go func(ctx context.Context) {   for {    select {     case <-ctx.Done():      fmt.Println(ctx.Value("uid"), "goroutine 已停止")      return    default:     fmt.Println("goroutine 正在运行")     time.Sleep(time.Second)    }   }  }(ctxValue)  time.Sleep(time.Second * 5)  cancel()  time.Sleep(time.Second * 5)  fmt.Println("main goroutine 已结束") }

阅读上面这段代码,我们使用 WithValue 函数给子 goroutine 传递上下文信息 uid。WithValue 函数接收三个参数,分别是  parent context,key 和 value。返回值是一个 context,我们可以在子 goroutine 中调用 Value  方法获取传递的上下文信息。

05、总结

本文我们简述了监控 goroutine 的几种方式,分别是 WaitGroup,chan + select 和 context。重点介绍了 context  的一些使用方式,分别是取消一个 goroutine,取消多个 goroutine  和传递上下文信息。关于定时取消和超时取消,感兴趣的读者可以参阅官方标准库文档。

“Golang语言中Context的使用方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. Golang的context包详解
  2. go语言中的context是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

golang

上一篇:idea中如何配置tomcat并且将项目部署到tomcat中

下一篇:SpringCloud高可用服务注册中心Eureka的用法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》