for-loop - 对于范围与静态 channel 长度 golang

标签 for-loop go concurrency

我有一个 channel 接收从日志文件解析的事件,另一个 channel 用于同步。出于我的测试目的,有 8 个事件。

使用 for range 时语法,我得到 4 个事件。当使用已知数 (8) 时,我可以得到所有的数。

func TestParserManyOpinit(t *testing.T) {
    ch := make(chan event.Event, 1000)
    done := make(chan bool)
    go parser.Parse("./test_data/many_opinit", ch, done)
    count := 0
    exp := 8
    evtList := []event.Event{}

    <-done
    close(ch)
    //This gets all the events
    for i := 0; i < 8; i++ {
            evtList = append(evtList, <-ch)
            count++
    }

    //This only gives me four
    //for range ch {
    //        evtList = append(evtList, <-ch)
    //        count++
    //}

    if count != exp || count != len(evtList) {
            t.Errorf("Not proper lenght, got %d, exp %d, evtList %d", count, exp, len(evtList))
    }

func Parse(filePath string, evtChan chan event.Event, done chan bool) {
    log.Info(fmt.Sprintf("(thread) Parsing file %s", filePath))
    file, err := os.Open(filePath)
    defer file.Close()

    if err != nil {
            log.Error("Cannot read file " + filePath)
    }
    count := 0
    scan := bufio.NewScanner(file)
    scan.Split(splitFunc)
    scan.Scan() //Skip log file header

    for scan.Scan() {
            text := scan.Text()
            text = strings.Trim(text, "\n")
            splitEvt := strings.Split(text, "\n")
            // Some parsing ...
            count++
            evtChan <- evt
    }

    fmt.Println("Done ", count) // gives 8
    done <- true
}

我一定是遗漏了与 channel 上的 for 循环相关的内容。

我试过添加 time.Sleep就在 done <- true 之前部分。它没有改变结果。

最佳答案

当您使用 for range 时,每次循环迭代都会从 channel 中读取,并且您不会使用读取的值。因此,一半的值被丢弃。应该是:

for ev := range ch {
        evtList = append(evtList, ev)
        count++
}

为了实际利用循环迭代器中读取的值。

Tour of Go 中演示了通过 channel 进行测距并在 Go spec 中详细说明.

关于for-loop - 对于范围与静态 channel 长度 golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52764633/

相关文章:

Python for-in 循环前面有一个变量

java - 执行代码N次和其他代码N+1次

r - 为整个数据集和计数在 R 中自动化 "for loop"

具有 bazel 构建的 protobuf 依赖项的 Google 云函数

c - 不确定我是否需要互斥锁

java - 如果您将对象分配给最终字段,其他线程是否会看到该对象的非最终/非 volatile 字段的先前更新?

Java 在多次成功执行线程后在循环中抛出 ExecutionException

java - 每个作用域初始化一次循环迭代器是否更好?

go - 如何让 gccgo 编译pig.go?

multithreading - 为什么我的代码在关闭 channel 和所有工作人员退出后仍然存在死锁?