在Golang,如何使用频道处理许多goroutine

时间:2017-12-28 01:10:33

标签: go

我想在Golang中使用for循环同时启动1000个goroutine   问题是:我必须确保每个goroutine都已被执行   是否可以使用渠道来帮助我确保这一点?

结构有点像这样:

func main {
    for i ... {
        go ...
        ch?
    ch?
}

3 个答案:

答案 0 :(得分:2)

正如@Andy所提到的,您可以使用sync.WaitGroup来实现这一目标。以下是一个例子。希望代码不言自明。

package main

import (
    "fmt"
    "sync"
    "time"
)

func dosomething(millisecs int64, wg *sync.WaitGroup) {
    defer wg.Done()
    duration := time.Duration(millisecs) * time.Millisecond
    time.Sleep(duration)
    fmt.Println("Function in background, duration:", duration)
}

func main() {
    arr := []int64{200, 400, 150, 600}
    var wg sync.WaitGroup
    for _, n := range arr {
     wg.Add(1)
     go dosomething(n, &wg)
    }
    wg.Wait()
    fmt.Println("Done")
}

答案 1 :(得分:1)

我建议您遵循一种模式。并发和通道是好的,但如果你以一种糟糕的方式使用它,你的程序可能会比预期更慢。处理多个go-routine和channel的简单方法是通过worker pool模式。

仔细查看下面的代码

// In this example we'll look at how to implement
// a _worker pool_ using goroutines and channels.

package main

import "fmt"
import "time"

// Here's the worker, of which we'll run several
// concurrent instances. These workers will receive
// work on the `jobs` channel and send the corresponding
// results on `results`. We'll sleep a second per job to
// simulate an expensive task.
func worker(id int, jobs <-chan int, results chan<- int) {
    for j := range jobs {
        fmt.Println("worker", id, "started  job", j)
        time.Sleep(time.Second)
        fmt.Println("worker", id, "finished job", j)
        results <- j * 2
    }
}

func main() {

    // In order to use our pool of workers we need to send
    // them work and collect their results. We make 2
    // channels for this.
    jobs := make(chan int, 100)
    results := make(chan int, 100)

    // This starts up 3 workers, initially blocked
    // because there are no jobs yet.
    for w := 1; w <= 3; w++ {
        go worker(w, jobs, results)
    }

    // Here we send 5 `jobs` and then `close` that
    // channel to indicate that's all the work we have.
    for j := 1; j <= 5; j++ {
        jobs <- j
    }
    close(jobs)

    // Finally we collect all the results of the work.
    for a := 1; a <= 5; a++ {
        <-results
    }
}

这个简单的例子取自here。此外,results频道可以帮助您跟踪执行作业的所有常规例程,包括失败通知。

答案 2 :(得分:0)

是的,请尝试this

package main

import (
    "fmt"
)

const max = 1000

func main() {
    for i := 1; i <= max; i++ {
        go f(i)
    }

    s := 0
    for i := 1; i <= max; i++ {
        s += <-ch
    }

    fmt.Println(s)
}

func f(n int) {
    // do a job here
    ch <- n
}

var ch = make(chan int, max)

输出:

500500