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 c语言数据结构实验单链表的删除
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline