Go中的Channel——range和select

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/wudawei071193/article/details/100370125

数据接受者总是面临这样的问题:何时停止等待数据?还会有更多的数据么,还是所有内容都完成了?我应该继续等待还是该做别的了?

对于该问题,一个可选的方式是,持续的访问数据源并检查channel是否已经关闭,但是这并不是高效的解决方式。Go提供了range关键字,将其使用在channel上时,会自动等待channel的动作一直到channel被关闭

 
  1. 示例代码1

  2. package main

  3. import (

  4. "fmt"

  5. "time"

  6. "strconv"

  7. )

  8.  
  9. func makeCakeAndSend(cs chan string, count int) {

  10. for i := 1; i <= count; i++ {

  11. cakeName := "Strawberry Cake " + strconv.Itoa(i)

  12. cs <- cakeName //send a strawberry cake

  13. }

  14. }

  15.  
  16. func receiveCakeAndPack(cs chan string) {

  17. for s := range cs {

  18. fmt.Println("Packing received cake: ", s)

  19. }

  20. }

  21.  
  22. func main() {

  23. cs := make(chan string)

  24. go makeCakeAndSend(cs, 5)

  25. go receiveCakeAndPack(cs)

  26.  
  27. //sleep for a while so that the program doesn’t exit immediately

  28. time.Sleep(3 * 1e9)

  29. }

 
  1. 输出结果

  2. Packing received cake: Strawberry Cake 1

  3. Packing received cake: Strawberry Cake 2

  4. Packing received cake: Strawberry Cake 3

  5. Packing received cake: Strawberry Cake 4

  6. Packing received cake: Strawberry Cake 5

我们告诉了蛋糕制作器我们需要5个蛋糕,但是蛋糕装箱器并不知道数目,而在之前版本的代码中,我们写死了具体的接收数目。上面的代码中,通过对channel使用range关键字,我们避免了给接收者写明要接收的数据个数这种不合理的需求——当channel被关闭时,接收者的for循环也被自动停止了

Channel and select

select关键字用于多个channel的结合,这些channel会通过类似于are-you-ready polling的机制来工作。select中会有case代码块,用于发送或接收数据——不论通过<-操作符指定的发送还是接收操作准备好时,channel也就准备好了。在select中也可以有一个default代码块,其一直是准备好的。那么,在select中,哪一个代码块被执行的算法大致如下:

  • 检查每个case代码块
  • 如果任意一个case代码块准备好发送或接收,执行对应内容
  • 如果多余一个case代码块准备好发送或接收,随机选取一个并执行对应内容
  • 如果任何一个case代码块都没有准备好,等待
  • 如果有default代码块,并且没有任何case代码块准备好,执行default代码块对应内容

在下面的程序中,我们扩展蛋糕制作工厂来模拟多于一种口味的蛋糕生产的情况——现在有草莓和巧克力两种口味!但是装箱机制还是同以前一样的。由于蛋糕来自不同的channel,而装箱器不知道确切的何时会有何种蛋糕放置到某个或多个channel上,这就可以用select语句来处理所有这些情况——一旦某一个channel准备好接收蛋糕/数据,select就会完成该对应的代码块内容

注意,我们这里使用的多个返回值case cakeName, strbry_ok := <-strbry_cs,第二个返回值是一个bool类型,当其为false时说明channel被关闭了。如果是true,说明有一个值被成功传递了。我们使用这个值来判断是否应该停止等待

 
  1. 示例代码2

  2. package main

  3.  
  4. import (

  5. "fmt"

  6. "time"

  7. "strconv"

  8. )

  9.  
  10. func makeCakeAndSend(cs chan string, flavor string, count int) {

  11. for i := 1; i <= count; i++ {

  12. cakeName := flavor + " Cake " + strconv.Itoa(i)

  13. cs <- cakeName //send a strawberry cake

  14. }

  15. close(cs)

  16. }

  17.  
  18. func receiveCakeAndPack(strbry_cs chan string, choco_cs chan string) {

  19. strbry_closed, choco_closed := false, false

  20.  
  21. for {

  22. //if both channels are closed then we can stop

  23. if (strbry_closed && choco_closed) { return }

  24. fmt.Println("Waiting for a new cake ...")

  25. select {

  26. case cakeName, strbry_ok := <-strbry_cs:

  27. if (!strbry_ok) {

  28. strbry_closed = true

  29. fmt.Println(" ... Strawberry channel closed!")

  30. } else {

  31. fmt.Println("Received from Strawberry channel. Now packing", cakeName)

  32. }

  33. case cakeName, choco_ok := <-choco_cs:

  34. if (!choco_ok) {

  35. choco_closed = true

  36. fmt.Println(" ... Chocolate channel closed!")

  37. } else {

  38. fmt.Println("Received from Chocolate channel. Now packing", cakeName)

  39. }

  40. }

  41. }

  42. }

  43.  
  44. func main() {

  45. strbry_cs := make(chan string)

  46. choco_cs := make(chan string)

  47.  
  48. //two cake makers

  49. go makeCakeAndSend(choco_cs, "Chocolate", 3) //make 3 chocolate cakes and send

  50. go makeCakeAndSend(strbry_cs, "Strawberry", 3) //make 3 strawberry cakes and send

  51.  
  52. //one cake receiver and packer

  53. go receiveCakeAndPack(strbry_cs, choco_cs) //pack all cakes received on these cake channels

  54.  
  55. //sleep for a while so that the program doesn’t exit immediately

  56. time.Sleep(2 * 1e9)

  57. }

 
  1. 输出结果

  2. Waiting for a new cake ...

  3. Received from Strawberry channel. Now packing Strawberry Cake 1

  4. Waiting for a new cake ...

  5. Received from Chocolate channel. Now packing Chocolate Cake 1

  6. Waiting for a new cake ...

  7. Received from Chocolate channel. Now packing Chocolate Cake 2

  8. Waiting for a new cake ...

  9. Received from Strawberry channel. Now packing Strawberry Cake 2

  10. Waiting for a new cake ...

  11. Received from Strawberry channel. Now packing Strawberry Cake 3

  12. Waiting for a new cake ...

  13. Received from Chocolate channel. Now packing Chocolate Cake 3

  14. Waiting for a new cake ...

  15. ... Strawberry channel closed!

  16. Waiting for a new cake ...

  17. ... Chocolate channel closed!

写在最后

实际上,有经验的Gopher一眼就能发现,示例代码1中的channel是没有正确关闭的,在for range语句的执行一直没有停止因为channel一直存在而没有被关闭,只不过随着time.Sleep()结束,main函数退出,所有的goroutine被关闭,该语句也被结束了而已

正确的解决步骤:
a)发送器一旦停止发送数据后立即关闭channel
b)接收器一旦停止接收内容,终止程序
c)移除time.Sleep语句

修改后代码:

 
  1. package main

  2.  
  3. import (

  4. "fmt"

  5. "strconv"

  6. )

  7.  
  8. func makeCakeAndSend(cs chan string, count int) {

  9. for i := 1; i <= count; i++ {

  10. cakeName := "Strawberry Cake " + strconv.Itoa(i)

  11. cs <- cakeName //send a strawberry cake

  12. }

  13. close(cs)

  14. }

  15.  
  16. func receiveCakeAndPack(cs chan string) {

  17. for s := range cs {

  18. fmt.Println("Packing received cake: ", s)

  19. }

  20. }

  21.  
  22. func main() {

  23. cs := make(chan string)

  24. go makeCakeAndSend(cs, 5)

  25. receiveCakeAndPack(cs)

  26. }

这样才是对channel使用range进行处理的优雅方法

同样的,第二个例子中,time.Sleep()语句可以去除掉,我们只需要让receiveCakeAndPack函数执行完毕后退出程序即可

修改后代码:

 
  1. package main

  2.  
  3. import (

  4. "fmt"

  5. "strconv"

  6. )

  7.  
  8. func makeCakeAndSend(cs chan string, flavor string, count int) {

  9. for i := 1; i <= count; i++ {

  10. cakeName := flavor + " Cake " + strconv.Itoa(i)

  11. cs <- cakeName //send a strawberry cake

  12. }

  13. close(cs)

  14. }

  15.  
  16. func receiveCakeAndPack(strbry_cs chan string, choco_cs chan string) {

  17. strbry_closed, choco_closed := false, false

  18.  
  19. for {

  20. //if both channels are closed then we can stop

  21. if strbry_closed && choco_closed {

  22. return

  23. }

  24. fmt.Println("Waiting for a new cake ...")

  25. select {

  26. case cakeName, strbry_ok := <-strbry_cs:

  27. if !strbry_ok {

  28. strbry_closed = true

  29. fmt.Println(" ... Strawberry channel closed!")

  30. } else {

  31. fmt.Println("Received from Strawberry channel. Now packing", cakeName)

  32. }

  33. case cakeName, choco_ok := <-choco_cs:

  34. if !choco_ok {

  35. choco_closed = true

  36. fmt.Println(" ... Chocolate channel closed!")

  37. } else {

  38. fmt.Println("Received from Chocolate channel. Now packing", cakeName)

  39. }

  40. }

  41. }

  42. }

  43.  
  44. func main() {

  45. strbry_cs := make(chan string)

  46. choco_cs := make(chan string)

  47.  
  48. //two cake makers

  49. go makeCakeAndSend(choco_cs, "Chocolate", 3) //make 3 chocolate cakes and send

  50. go makeCakeAndSend(strbry_cs, "Strawberry", 4) //make 3 strawberry cakes and send

  51.  
  52. //one cake receiver and packer

  53. receiveCakeAndPack(strbry_cs, choco_cs) //pack all cakes received on these cake channels

  54. }

猜你喜欢

转载自blog.csdn.net/wudawei071193/article/details/100370125