dongyuan4790 2019-03-10 22:40
浏览 67
已采纳

从频道切片中选择(发送到免费频道)?

I'm making multiples http request:

    type item struct{
       me []byte
    }
    items := getItems()
    for _, me := range items {
          me.save()
    }

In order to do it efficiently I'm doing it with go rutines, my first approach was to make it like a pool of go rutines:

    items := getItems()
    var wg sync.WaitGroup
    wg.Add(len(items))
    for _, me := range items {
        go func(me item) {
            me.save()
            wg.Done()
        }(me)
    }
    wg.Wait()

But they all try to make http request at the same time and some of them fail because of my bandwidth can't handle them all. So I try channels along with select instead :

    channel1 := make(chan item)
    channel2 := make(chan item)
    channel3 := make(chan item)
    var wg sync.WaitGroup
    items := getItems()
    wg.Add(len(items))
    go func() {
        for me := range channel1 {
            me.save()
            wg.Done()
        }
    }()
    go func() {
        for me := range channel2 {
            me.save()
            wg.Done()
        }
    }()
    go func() {
        for me := range channel3 {
            me.save()
            wg.Done()
        }
    }()
    for _, me := range items {
        select {
        case channel1 <- me:
        case channel2 <- me:
        case channel3 <- me:
        }
    }

But adding more go rutines to find the max go rutines my bandwidht can handle, my code gets larger and larger, and I try to do this:

    max:=7
    var channels []chan item
    for i:=0;i<max;i++{
        channel=make(chan item)
        channels=append(channels,channel)
    }
    for _, me := range items {
        select {
            //???????????????
        }
    }

but I'm not prety sure how to do it as the last approach

Also take in mind that "Select from slice of channels" was a question already asked, but their answers were only when select is listening which channel arrive first, in my case I want Select to send any free channel, so it's different

  • 写回答

2条回答 默认 最新

  • dongyin4202 2019-03-23 22:03
    关注

    I had a wrong approach and the answer was easy with worker pools

    type item struct {
        me []byte
    }
    
    func worker(canalFiles <-chan item, wg *sync.WaitGroup) {
        for file := range canalFiles {
            file.save()
            wg.Done()
        }
    }
    func main() {
        var wg sync.WaitGroup
        items := getItems()
        wg.Add(len(items))
        canalFiles := make(chan item)
        for i := 0; i < 8; i++ {
            go worker(canalFiles, &wg)
        }
        for _, file := range items {
            canalFiles <- file
        }
        fmt.Printf("waiting.....")
        wg.Wait()
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?