douli1306
2018-08-08 07:32
浏览 119
已采纳

Golang:在多个goroutine中发送关闭通道错误

I have 3 channels that get data from together . My code run for first time perfect but when after that I send another data , there is error : send on closed channel.In func FillNotCheckedDeliveryCh that line I closed channel .If I do not close it app stopped and not continue.

func main() {
    receiveBulkIdsCh := make(chan int64, 100)
    NotCheckedDeliveryCh := make(chan CommonType.BasicRequestParameters, 100)
    ResultCh := make(chan CommonType.MessageStateResult, 100)

    var wg sync.WaitGroup
    wg.Add(4)

    /* Every BulkIds that receive from queue , save in  receiveBulkIdsChan channel*/
    go func() {
        defer wg.Done()
        PopQueue(receiveBulkIdsCh)
    }()
    go func() {
        defer wg.Done()
        for BulkId := range receiveBulkIdsCh {
            FillNotCheckedDeliveryCh(data, NotCheckedDeliveryCh)
        }
    }()
    go func() {
        defer wg.Done()
        for item := range NotCheckedDeliveryCh {
            for msgStatus := range DoFuncGetMessageState(item) {
                ResultCh <- msgStatus
            }
            close(ResultCh)
        }
    }()
    go func() {
        defer wg.Done()
        for Result := range ResultCh {
            ReadResultCh(Result)
        }
    }()
    wg.Wait()
}

func FillNotCheckedDeliveryCh(data IntegrateRowsFields, ch chan<- CommonType.BasicRequestParameters) {
    ch <- PackerForNotFinishedCh(data)
    defer close(ch)
}

can help what's wrong ?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douya1248 2018-08-08 07:54
    已采纳

    When you work with channels in Go always the sender should close the channel. Because that signals that no more data will be send over the channel.

    In your code the receiver is closing it. Just leave the channel here opened and remove the close(ch) there.

    func FillNotCheckedDeliveryCh(data IntegrateRowsFields, ch chan<- CommonType.BasicRequestParameters) {
        ch <- PackerForNotFinishedCh(data)
        defer close(ch)
    }
    

    If something is blocking at that part you could use select:

    func FillNotCheckedDeliveryCh(data IntegrateRowsFields, ch chan<- CommonType.BasicRequestParameters) {  
        select {
          case ch <- PackerForNotFinishedCh(data):
          default:
        }
    }
    
    已采纳该答案
    打赏 评论
  • dquv73115 2018-08-08 09:17

    Closing the channels on the receiver side, and then trying to send on the same channel gives you the error. Better is to close the channel outside the go routine only after all values are send on the channels.

    Close the channel after waiting for all go routines to be finished.

    wg.Wait()
    close(ch)
    

    Always close the channel when there are no more values to send on that channel.

    Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression: after

    v, ok := <-ch
    

    In your case too you can check on the receiver side by using ok if there are more values in the channel or if it is closed.

    Channels aren't like files; you don't usually need to close them. Closing is only necessary when the receiver must be told there are no more values coming, such as to terminate a range loop.

    Go playground example for generating the error when not closing a channel and range over it.

    Playground example for closing the channels when all values are send.

    打赏 评论

相关推荐 更多相似问题