dongshi3361 2015-07-08 03:03
浏览 2
已采纳

有条件的例程/通道

I would like to make the statistics routine conditional so that it only runs on certain cases otherwise it will waste cycles half the time. Right now I have one go routine act as a producer to feed the two consumer routines via buffered channels. Is there a way I can make it so the statistics routine is conditional or is there a better pattern I should follow? Thanks in advance for any and all help!

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条回答 默认 最新

  • dongposhi8677 2015-07-08 03:13
    关注

    There is nothing wrong with making this conditional:

    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
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?