dongzuo4666 2015-05-01 08:52
浏览 3
已采纳

前往频道:如何使其不受阻碍?

I have a script which is selecting some data from a database and sending this to a channel to be processed by multiple goroutines, with the results then sent back to the main thread to be updated on the database.

However, it is hanging (probably blocking) on sending the data to first channel.

The channels are created globally with:

var chin = make(chan in)
var chout = make(chan out)

in and out are both structs

First the goroutines are started:

for i:=0; i<5; i++ {
     go worker()
}

The code to load the channels is then:

        if verbose {
            fmt.Println(`Getting nextbatch2 and sending to workers`)
        }

        rows, err = nextbatch2.Query()
        if err != nil {
            panic(err)
        }

        var numtodo int
        for rows.Next() {
            err = rows.Scan(&id, &data)
            if err != nil {
                rows.Close()
                panic(err)
            }

            // Start
            var vin in
            vin.id = id
            vin.data = data
            chin <- vin
            numtodo++
        }
        rows.Close()

Then immediately after this:

        if verbose {
            fmt.Println(`Processing out channel from workers`)
        }

        for res := range chout {
            update5.Exec(res.data, res.id)
            if numtodo--; numtodo == 0 {
                break
            }
        }

And in the background are multiple worker() goroutines running:

func worker() {
      for res := range chin {
            var v out
            v.id = res.id
            v.data = process(res.data)
            chout <- v
      }
}

This code hangs after printing Getting nextbatch2 and sending to workers. It never gets to Processing out channel from workers. So it's hanging somewhere inside the rows.Next() loop, for which I can't figure out the reason as the chin channel should be non-blocking - even if the worker() goroutines were not processing it should still at least finish that loop.

Any ideas?

EDIT:

By adding on fmt.Println(" on", numtodo) at the end of the rows.Next() loop I can see that it blocks after 5, which I don't understand as it should be non-blocking, right?

EDIT 2:

By changing the channels to make(chan in/out, 100) it now will block after 105.

  • 写回答

1条回答 默认 最新

  • dongyao5843 2015-05-01 09:20
    关注

    Receivers always block until there is data to receive. If the channel is unbuffered, the sender blocks until the receiver has received the value.

    https://golang.org/doc/effective_go.html#channels

    So you can rewrite your consumer-code to something like:

    go func(){
        for res := range chout {
            update5.Exec(res.data, res.id)
        }
    }()
    

    Also you need to close(chin) and close(chout) for proper usage of range statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭