duanleixun2439 2016-08-26 20:25
浏览 23
已采纳

从golang频道中依次读取

I'm trying to achieve parallel processing and communication over the channels in go.

What I basically try to solve is process a specifc data in parallel, and get results in order => introduced type Chunk for the purpose (see bellow).

I just make new channel for each chunk processing and keep them in slice => expect to be ordered once I iterate over them afterwards.

Simplified version of my program is (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
", c.from, c.to), nil}

        close(outChannel)
    }()

    return outChannel
}

The output I see is:

[STDOUT] from: 2 to: 4
[STDOUT] from: 2 to: 4

What I'd however expect to see would be:

[STDOUT] from: 0 to: 2
[STDOUT] from: 2 to: 4

What am I doing wrong here? I don't see it.

  • 写回答

1条回答 默认 最新

  • douya2433 2016-08-26 20:37
    关注

    The trouble is in the very first for loop of main. When you use for range loop, the loop variable (chunk here) gets created once and is assigned a copy of each slice element per iteration.

    When you call processChunk(&chunk), you are passing the address of this loop variable, and the value of this variable changes with each iteration. Thus the function processChunk always ends up working on the last item in the chunks loop since that is what *chunk points to after the for loop finishes.

    To fix, use slice indexing:

    for i := 0; i < len(chunks); i++ {
        // pass chunk objects by indexing chunks
        outChannels = append(outChannels, processChunk(&chunks[i]))
    }
    

    Fixed code: https://play.golang.org/p/A1_DtkncY_

    You can read more about range here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100