为了扩展对先前答案的一些评论并在此处提供更清楚的比较,此处给出了迄今为止给出的两种方法的示例,这些方法在相同的输入,读取的通道片和调用每个值的函数的情况下也需要知道哪个价值的渠道。
两种方法之间存在三个主要区别:
复杂。尽管这可能部分是读者的偏爱,但我发现频道方法更惯用,直截了当且更具可读性。
性能。在我的Xeon amd64系统上,goroutines + channels执行反射解决方案的速度大约为两个数量级(通常,Go中的反射速度通常较慢,应仅在绝对需要时使用)。当然,如果在函数处理结果或将值写入输入通道中存在任何明显的延迟,则性能差异很容易变得无关紧要。
阻塞/缓冲语义。其重要性取决于用例。通常,这无关紧要,或者goroutine合并解决方案中的少量额外缓冲可能有助于提高吞吐量。但是,如果希望具有这样的语义,即只有一个写程序被解除阻塞,并且其值在任何其他写程序被解除阻塞之前已得到充分处理,那么这只能通过反射解决方案来实现。
注意,如果不需要发送通道的“ id”或源通道永远不会关闭,则可以简化这两种方法。
Goroutine合并频道:
// Process1 calls `fn` for each value received from any of the `chans`
// channels. The arguments to `fn` are the index of the channel the
// value came from and the string value. Process1 returns once all the
// channels are closed.
func Process1(chans []<-chan string, fn func(int, string)) {
// Setup
type item struct {
int // index of which channel this came from
string // the actual string item
}
merged := make(chan item)
var wg sync.WaitGroup
wg.Add(len(chans))
for i, c := range chans {
go func(i int, c <-chan string) {
// Reads and buffers a single item from `c` before
// we even know if we can write to `merged`.
//
// Go doesn't provide a way to do something like:
// merged <- (<-c)
// atomically, where we delay the read from `c`
// until we can write to `merged`. The read from
// `c` will always happen first (blocking as
// required) and then we block on `merged` (with
// either the above or the below syntax making
// no difference).
for s := range c {
merged <- item{i, s}
}
// If/when this input channel is closed we just stop
// writing to the merged channel and via the WaitGroup
// let it be known there is one fewer channel active.
wg.Done()
}(i, c)
}
// One extra goroutine to watch for all the merging goroutines to
// be finished and then close the merged channel.
go func() {
wg.Wait()
close(merged)
}()
// "select-like" loop
for i := range merged {
// Process each value
fn(i.int, i.string)
}
}
反射选择:
// Process2 is identical to Process1 except that it uses the reflect
// package to select and read from the input channels which guarantees
// there is only one value "in-flight" (i.e. when `fn` is called only
// a single send on a single channel will have succeeded, the rest will
// be blocked). It is approximately two orders of magnitude slower than
// Process1 (which is still insignificant if their is a significant
// delay between incoming values or if `fn` runs for a significant
// time).
func Process2(chans []<-chan string, fn func(int, string)) {
// Setup
cases := make([]reflect.SelectCase, len(chans))
// `ids` maps the index within cases to the original `chans` index.
ids := make([]int, len(chans))
for i, c := range chans {
cases[i] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(c),
}
ids[i] = i
}
// Select loop
for len(cases) > 0 {
// A difference here from the merging goroutines is
// that `v` is the only value "in-flight" that any of
// the workers have sent. All other workers are blocked
// trying to send the single value they have calculated
// where-as the goroutine version reads/buffers a single
// extra value from each worker.
i, v, ok := reflect.Select(cases)
if !ok {
// Channel cases[i] has been closed, remove it
// from our slice of cases and update our ids
// mapping as well.
cases = append(cases[:i], cases[i+1:]...)
ids = append(ids[:i], ids[i+1:]...)
continue
}
// Process each value
fn(ids[i], v.String())
}
}
[ Go游乐场上的完整代码。]