有条件的Go例程/频道

时间:2015-07-08 03:03:48

标签: go producer-consumer

我想使统计例程有条件,以便它只在某些情况下运行,否则会浪费一半的时间。现在我有一个例行程序作为生产者通过缓冲渠道提供两个消费者例程。我有没有办法让它成为统计例程是有条件的还是我应该遵循更好的模式?在此先感谢您的帮助!

func main() {
    options()
    go produce(readCSV(loc))
    go process()
    go statistics() // only on flag
    <-done
}

func produce(entries [][]string) {
    regex, err := regexp.Compile(reg)
    if err != nil {
        log.Error(reg + ", is not a valid regular expression")
    } else {
        for _, each := range entries {
            if regex.MatchString(each[col]) {
                matches <- each
                stats <- each // only on flag
            }
        }
    }
    done <- true
}

func process() {
    for {
        match := <-matches
        if len(match) != 0 {
            // PROCESS
        }
    }
}

func statistics() {
    for {
        stat := <-stats
        if len(stat) != 0 {
            // STATISTICS
        }
    }
}

3 个答案:

答案 0 :(得分:1)

制作此条件没有任何问题:

var stats chan []string  // Don't initialize stats.

func main() {
    options()
    go produce(readCSV(loc))
    go process()
    if flag {
        stats = make(chan []string, 1024)
        go statistics() // only on flag
    }
    <-done
}

func produce(entries [][]string) {
    regex, err := regexp.Compile(reg)
    if err != nil {
        log.Error(reg + ", is not a valid regular expression")
    } else {
        for _, each := range entries {
            if regex.MatchString(each[col]) {
                matches <- each
                if stats != nil {
                    stats <- each // only on flag
                }
            }
        }
    }
    close(done)
}

func process() {
    for {
        select {
        case match := <-matches:
            if len(match) != 0 {
              // PROCESS
            }
        case <-done:
            return
        }
    }
}

func statistics() {
    for {
        select {
        case stat := <-stats:
            if len(stat) != 0 {
                // STATISTICS
            }
        case <-done:
            return
        }
    }
}

答案 1 :(得分:0)

也许你正在寻找旗帜包。

import "flag"

var withStats = flag.Boolean("s", false, "Do statistics")

func main() {
    flag.Parse()
    ...
    if *withStats == true {
        t := statType
        size := 100
        stats := make(chan, t, size)
        go statistics() // only on flag
    }
    ...
}

答案 2 :(得分:0)

如果您从代码中的许多位置更新统计信息,则可能需要添加一些辅助方法。类似的东西:

type stats struct {
    ch chan []string
}

func (s *stats) update(a []string) {
    if s != nil {
        s.ch <- a
    }
}

func (s *stats) start() {
    if s != nil {
        s.ch = make(chan []string)
        go statistics()
    }
}

var s *stats
if enabled {
    s = new(stats)
}
s.start()

// later in the code
s.update(each)
相关问题