我正在尝试通过 go 中的通道实现并行处理和通信。
我基本上试图解决的是并行处理特定数据,并按顺序获得结果=>Chunk为此目的引入类型(见下文)。
我只是为每个块处理创建新通道并将它们保存在切片中 => 期望在我之后迭代它们时被订购。
我的程序的简化版本是(https://play.golang.org/p/RVtDGgUVCV):
package main
import (
"fmt"
)
type Chunk struct {
from int
to int
}
func main() {
chunks := []Chunk{
Chunk{
from: 0,
to: 2,
},
Chunk{
from: 2,
to: 4,
},
}
outChannels := [](<-chan struct {
string
error
}){}
for _, chunk := range chunks {
outChannels = append(outChannels, processChunk(&chunk))
}
for _, outChannel := range outChannels {
for out := range outChannel {
if out.error != nil {
fmt.Printf("[ERROR] %s", out.error)
return
}
fmt.Printf("[STDOUT] %s", out.string)
}
}
}
func processChunk(c *Chunk) <-chan struct {
string
error
} {
outChannel := make(chan struct {
string
error
})
go func() {
outChannel <- struct {
string
error
}{fmt.Sprintf("from: %d to: %d\n", c.from, c.to), nil}
close(outChannel)
}()
return outChannel
}
我看到的输出是:
[STDOUT] from: 2 to: 4
[STDOUT] from: 2 to: 4
然而,我希望看到的是:
[STDOUT] from: 0 to: 2
[STDOUT] from: 2 to: 4
我在这里做错了什么?我没看到。
MMTTMM
相关分类