代码之家  ›  专栏  ›  技术社区  ›  John Balvin Arias

从频道片中选择(发送到自由频道)?

  •  0
  • John Balvin Arias  · 技术社区  · 7 年前

    我正在进行多次http请求:

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

    为了有效地完成这项工作,我使用go-rutines,我的第一个方法是使它像一个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()
    

    select 取而代之的是:

        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:
            }
        }
    

    但是添加更多的go-rutines以找到我的bandwidht可以处理的最大go-rutines,我的代码变得越来越大,我尝试这样做:

        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 {
                //???????????????
            }
        }
    

    但我不知道最后一种方法是怎么做的

    还要记住,“从一片频道中选择”是一个已经被问到的问题,但他们的答案只有在 选择 在听哪个频道先到,在我的情况下我想 Select 发送任何免费频道,所以它是不同的

    0 回复  |  直到 7 年前
        1
  •  1
  •   Juan Cespedes    7 年前

    你可以用 reflect.Select ,创建 SelectCase Dir=SelectSend ,如下所示:

    max:=7
    var channels []chan item
    for i:=0;i<max;i++{
        channel=make(chan item)
        channels=append(channels,channel)
    }
    for _, me := range items {
        cases := make([]reflect.SelectCase, max)
        for j := 0; j < max; j++ {
            cases[j] = reflect.SelectCase{
                Dir: reflect.SelectSend,
                Chan: reflect.ValueOf(channels[j]),
                Send: reflect.ValueOf(me)
            }
        }
        reflect.Select(cases)
    }
    
        2
  •  0
  •   John Balvin Arias    7 年前

    我有一个错误的方法和答案是容易的 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()
    }