dongyuyi5680 2015-10-28 20:00
浏览 92
已采纳

golang惯用的方式来停止

I'm new to Go so I apologize in advance if the answer to my question is obvious :)

I'm planning a producer that reads a file and send each line to a channel, like:

scanner := bufio.NewScanner(file)
for scanner.Scan() {
    processingChan <- scanner.Text()
}

and add some goroutines to consume the lines.

now, what I want is that if ANY line fails to process in a goroutine (let's say the line contains an invalid value for my business rules), I want to stop the producer loop, close the file (already defered) and finish the program.

the question is: how can I "notify" the producer loop/for to stop?

I found someone suggesting:

for scanner.Scan() {
    select {
    case <- quit:
        // break / return
    default:
        // send next line to channel
    }
}

and the consumer goroutines would write to a "quit" (or error) channel in case of any fault.

this approach possibly solves the question, but I wonder if there is a cleaner/better or just common/popular approach.

  • 写回答

3条回答 默认 最新

  • duanliu6083 2015-10-28 22:35
    关注

    Correct, use the quit channel. Especially as you're already sending to the channel in the loop, handling additional one is easy. However, I wouldn't use the form you proposed, but simpler and safer version:

    for scanner.Scan() {
        select {
        case <- quit:
            return
        case processingChan <- scanner.Text():
        }
    }
    

    Why is it safer? Because it doesn't deadlock, contrary to your example with default. You might be lucky and never encounter it, but there're scenarios where you will. The problem lies in the fact you have two routines talking to each other, which always needs a little bit more of attention. Consider this:

    quit := make(chan error, 1)
    prod := make(chan int)
    
    go func() {
        for n := range prod {
            runtime.Gosched()
            if n%66 == 0 {
                quit <- errors.New("2/3 of evil")
                return
            }
        }
    }()
    
    for n := 1; n < 1000; n++ {
        select {
        case <-quit:
            fmt.Println(n)
            return
        default:
            prod <- n
        }
    }
    

    // https://play.golang.org/p/3kDRAAwaKR

    Boom! Main routine is trying to send to prod channel, but there is nobody to receive it; the same issue with our consumer.

    Adding buffers to the channels won't solve the problem either, but would make it less likely.

    Compare the previous example with the following change:

    select {
    case <-quit:
        fmt.Println(n)
        return
    case prod <- n:
    }
    

    // https://play.golang.org/p/pz8DMYdrVV

    Works nicely.

    I understand one would like to use the first option to make sure they're quitting as early as possible, but it's usually not a massive issue if you send one or two additional items for processing before exiting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面