使用golang频道的结果不一致

时间:2013-12-18 07:39:27

标签: go channel goroutine

我用Go编写的任务从一堆文本文件中获取一个唯一的列表。我使用通道进行了一些并行化,现在结果不一致 - 每次使用相同的输入文件时输出/输出的差异为5。

我在Fedora x86_64上使用go run process.go | wc -l测试它,go1.1.2,8核心amd。

代码是:

package main

import (
    "fmt"
    "os"
    "io"    
    "encoding/csv"
    "regexp"
    "log"
)

var (
    cleanRe *regexp.Regexp = regexp.MustCompile("[^0-9]+")
    comma rune ='\t'
    fieldsPerRecord=-1
)

func clean(s string) string {
    clean:=cleanRe.ReplaceAllLiteralString(s,"")
    if len(clean)<6 {return ""}
    return clean
}

func uniqueChannel(inputChan chan []string, controlChan chan string) {
    defer func(){controlChan<-"Input digester."}()
    uniq:=make(map[string]map[string]bool)
    i:=0
    for record:= range inputChan {
        i++
        id,v:=record[0],record[1]
        if uniq[id]==nil {
            uniq[id]=make(map[string]bool)
        } else if !uniq[id][v] {
            uniq[id][v]=true
            fmt.Println(id,string(comma),v)
        }
    }
    log.Println("digest ", i)
}

func processFile(fileName string, outputChan chan []string, controlChan chan string) {
    defer func(){controlChan<-fileName}()
    f,err:=os.Open(fileName)
    if err!=nil{log.Fatal(err)}
    r:=csv.NewReader(f)
    r.FieldsPerRecord = fieldsPerRecord
    r.Comma = comma

    //  Process the records
    i:=0
    for record,err:=r.Read();err!=io.EOF;record,err=r.Read() {
        if err!=nil{continue}
        id:=record[0]
        for _,v:=range record[1:] {
            if cleanV:=clean(v);cleanV!=""{
                i++
                outputChan<-[]string{id,cleanV}
            }
        }
    }
    log.Println(fileName,i)
}


func main() {
    inputs:=[]string{}
    recordChan:=make(chan []string,100)
    processesLeft:=len(inputs)+1
    controlChan:=make(chan string,processesLeft)

    //  Ingest the inputs
    for _,fName:=range inputs {
        go processFile(fName,recordChan,controlChan)
    }

    //  This is the loop to ensure it's all unique
    go uniqueChannel(recordChan,controlChan)

    //  Make sure all the channels close up
    for processesLeft>0 {
        if processesLeft==1{
            close(recordChan)
        }
        c:=<-controlChan
        log.Println(c)
        processesLeft--
    }
    close(controlChan)
}

似乎它在空洞之前关闭了频道。没有关闭机制,我就陷入僵局 - 我没有想法。

1 个答案:

答案 0 :(得分:1)

您可以放弃控制频道并使用sync.WaitGroup

package main

import (
    "encoding/csv"
    "fmt"
    "io"
    "log"
    "os"
    "regexp"
    "sync"
)

var (
    cleanRe         *regexp.Regexp = regexp.MustCompile("[^0-9]+")
    comma           rune           = '\t'
    fieldsPerRecord                = -1
)

func clean(s string) string {
    clean := cleanRe.ReplaceAllLiteralString(s, "")
    if len(clean) < 6 {
        return ""
    }
    return clean
}

func uniqueChannel(inputChan chan []string) {
    uniq := make(map[string]map[string]bool)
    i := 0
    for record := range inputChan {
        i++
        id, v := record[0], record[1]
        if uniq[id] == nil {
            uniq[id] = make(map[string]bool)
        } else if !uniq[id][v] {
            uniq[id][v] = true
            fmt.Println(id, string(comma), v)
        }
    }
    log.Println("digest ", i)
}

func processFile(fileName string, outputChan chan []string) {
    f, err := os.Open(fileName)
    if err != nil {
        log.Fatal(err)
    }
    r := csv.NewReader(f)
    r.FieldsPerRecord = fieldsPerRecord
    r.Comma = comma

    //  Process the records
    for record, err := r.Read(); err != io.EOF; record, err = r.Read() {
        if err != nil {
            continue
        }
        id := record[0]
        for _, v := range record[1:] {
            if cleanV := clean(v); cleanV != "" {
                outputChan <- []string{id, cleanV}
            }
        }
    }
}

func main() {
    inputs := []string{"ex.tsv"}
    recordChan := make(chan []string)

    var wg sync.WaitGroup
    //  Ingest the inputs
    for _, fName := range inputs {
        wg.Add(1)
        go func() {
            processFile(fName, recordChan)
            wg.Done()
        }()
    }
    go func() {
        wg.Wait()
        close(recordChan)
    }()

    //  This is the loop to ensure it's all unique
    uniqueChannel(recordChan)
}