Channel使用技巧

939496716 · · 2070 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

[原文地址](https://bingjian-zhu.github.io/2019/09/25/channel%E4%BD%BF%E7%94%A8%E6%8A%80%E5%B7%A7/) #### 前言 Go协程一般使用channel(通道)通信从而协调/同步他们的工作。合理利用Go协程和channel能帮助我们大大提高程序的性能。本文将介绍一些使用channel的场景及技巧 #### 场景一,使用channel返回运算结果 计算斐波那契数列,在学习递归时候这是个经典问题。现在我们不用递归实现,而是用channel返回计算得出的斐波那契数列。 计算前40个斐波那契数列的值,看下效率 ```go package main import ( "fmt" "time" ) //计算斐波那契数列并写到ch中 func fibonacci(n int, ch chan<- int) { first, second := 1, 1 for i := 0; i < n; i++ { ch <- first first, second = second, first+second } close(ch) } func main() { ch := make(chan int, 40) i := 0 start := time.Now() go fibonacci(cap(ch), ch) for result := range ch { fmt.Printf("fibonacci(%d) is: %d\n", i, result) i++ } end := time.Now() delta := end.Sub(start) fmt.Printf("took the time: %s\n", delta) } ``` 只花了7ms,效率是递归实现的100倍(主要是算法效率问题) ``` fibonacci(33) is: 5702887 fibonacci(34) is: 9227465 fibonacci(35) is: 14930352 fibonacci(36) is: 24157817 fibonacci(37) is: 39088169 fibonacci(38) is: 63245986 fibonacci(39) is: 102334155 took the time: 8.0004ms ``` >使用for-range读取channel返回的结果十分便利。当channel关闭且没有数据时,for循环会自动退出,无需主动监测channel是否关闭。close(ch)只针对写数据到channel起作用,意思是close(ch)后,ch中不能再写数据,但不影响从ch中读数据 #### 场景二,使用channel获取多个并行方法中的一个结果 假设程序从多个复制的数据库同时读取。只需要接收首先到达的一个答案,Query 函数获取数据库的连接切片并请求。并行请求每一个数据库并返回收到的第一个响应: ```go func Query(conns []conn, query string) Result { ch := make(chan Result, 1) for _, conn := range conns { go func(c Conn) { select { case ch <- c.DoQuery(query): } }(conn) } return <- ch } ``` #### 场景三,响应超时处理 在调用远程方法的时候,存在超时可能,超时后返回超时提示 ```go func CallWithTimeOut(timeout time.Duration) (int, error) { select { case resp := <-Call(): return resp, nil case <-time.After(timeout): return -1, errors.New("timeout") } } func Call() <-chan int { outCh := make(chan int) go func() { //调用远程方法 }() return outCh } ``` >同样可以扩展到channel的读写操作 > ```go func ReadWithTimeOut(ch <-chan int) (x int, err error) { select { case x = <-ch: return x, nil case <-time.After(time.Second): return 0, errors.New("read time out") } } func WriteWithTimeOut(ch chan<- int, x int) (err error) { select { case ch <- x: return nil case <-time.After(time.Second): return errors.New("read time out") } } ``` >使用<-time.After()超时设置可能引发的内存泄露问题,可以看[这篇文章](https://studygolang.com/articles/22617) #### 场景四,多任务并发执行和顺序执行 方法A和B同时执行,方法C等待方法A执行完后才能执行,main等待A、B、C执行完才退出 ```go package main import ( "fmt" "time" ) func B(quit chan<- string) { fmt.Println("B crraied out") quit <- "B" } func A(quit chan<- string, finished chan<- bool) { // 模拟耗时任务 time.Sleep(time.Second * 1) fmt.Println("A crraied out") finished <- true quit <- "A" } func C(quit chan<- string, finished <-chan bool) { // 在A没有执行完之前,finished获取不到数据,会阻塞 <-finished fmt.Println("C crraied out") quit <- "C" } func main() { finished := make(chan bool) defer close(finished) quit := make(chan string) defer close(quit) go A(quit, finished) go B(quit) go C(quit, finished) fmt.Println(<-quit) fmt.Println(<-quit) fmt.Println(<-quit) } ``` 正常执行我们得到以下结果 ``` B crraied out B A crraied out A C crraied out C ``` >注意:最后从quit中读数据不能使用for-range语法,不然程序会出现死锁 > ```go for res := range quit { fmt.Println(res) } ``` >``` >fatal error: all goroutines are asleep - deadlock! >``` >原因很简单,程序中quit通道没有被close,A、B、C运行完了,Go的主协程在for循环中阻塞了,所有Go协程都阻塞了,进入了死锁状态 #### 场景五,超时后停止Go协程,避免浪费资源(停止调用链) 场景四中,假设A方法挂了或者需要执行很长时间,main协程会等到所有方法执行完才会退出。在实际应用中显然不行,所以要设置超时时间。问题来了,C方法是基于A方法执行完后才执行的,我们怎样通知C方法退出呢。这里针对普通的Go协程,不是Http请求,有关Http超时问题引起的内存泄露可以看[这篇文章](https://juejin.im/post/5d66206b6fb9a06aed7132af?utm_campaign=studygolang.com&utm_medium=studygolang.com&utm_source=studygolang.com) 下面我们修改场景四的代码,让A方法有超时设置,C方法在A方法超时后也退出 ```go package main import ( "fmt" "time" ) // B方法 func B(quit chan<- string) { fmt.Println("B crraied out") quit <- "B" } // A方法,有超时限制 func AWithTimeOut(quit chan<- string, finishedA chan<- bool, timeout time.Duration) { select { case resp := <-A(finishedA): quit <- resp case <-time.After(timeout): quit <- "A timeout" } } // A需要执行的任务 func A(finishedA chan<- bool) <-chan string { respCh := make(chan string) go func() { // 模拟耗时任务 // time.Sleep(time.Second * 3) fmt.Println("A crraied out") finishedA <- true respCh <- "A" }() return respCh } // C方法,等待A方法完成后才能执行,同样有超时限制,超时时间和A方法一致 func CWithTimeOut(quit chan<- string, finishedA <-chan bool, timeout time.Duration) { select { case <-finishedA: fmt.Println("C crraied out") quit <- "C" case <-time.After(timeout): fmt.Println("C Exited") quit <- "C timeout" } } func main() { finishedA := make(chan bool, 1) //这里必须要是1的缓冲通道,不然超时后会死锁 defer close(finishedA) quit := make(chan string, 3) defer close(quit) timeout := time.Second * 2 go AWithTimeOut(quit, finishedA, timeout) go B(quit) go CWithTimeOut(quit, finishedA, timeout) fmt.Println(<-quit) fmt.Println(<-quit) fmt.Println(<-quit) time.Sleep(time.Second * 3) //如果程序未退出的话,A方法执行的任务还会继续运行,因为我们没办法让A方法停下来 } ``` 运行结果 ``` B crraied out B C Exited C timeout A timeout A crraied out ``` > A方法用`time.Sleep(time.Second * 3)`模拟超时任务,代码最后让main协程休眠,主要为了说明虽然A超时了,但正常情况下它还是会把任务执行下去的。如果有哪位大侠有什么方法能让它不执行,还请告知!!! ### 总结 本文介绍了几种场景下channel的使用技巧,希望能起到抛砖引玉的作用,各位如有其它技巧,欢迎评论,本文会把你们的技巧收纳在其中。感谢!!!

有疑问加站长微信联系(非本文作者))

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2070 次点击  
加入收藏 微博
被以下专栏收入,发现更多相似内容
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传