douchuopiao9002 2016-02-21 23:35
浏览 21
已采纳

在Go中实现作业工人池

Since Go does not have generics, all the premade solutions use type casting which I do not like very much. I also want to implement it on my own and tried the following code. However, sometimes it does not wait for all goroutines, am I closing the jobs channel prematurely? I do not have anything to fetch from them. I might have used a pseudo output channel too and waited to fetch the exact amount from them however I believe the following code should work too. What am I missing?

func jobWorker(id int, jobs <-chan string, wg sync.WaitGroup) {
    wg.Add(1)
    defer wg.Done()

    for job := range jobs {
        item := ParseItem(job)
        item.SaveItem()
        MarkJobCompleted(item.ID)
        log.Println("Saved", item.Title)
    }
}

// ProcessJobs processes the jobs from the list and deletes them
func ProcessJobs() {

    jobs := make(chan string)

    list := GetJobs()
    // Start workers
    var wg sync.WaitGroup
    for w := 0; w < 10; w++ {
        go jobWorker(w, jobs, wg)
    }

    for _, url := range list {
        jobs <- url
    }

    close(jobs)
    wg.Wait()
}
  • 写回答

2条回答 默认 最新

  • dougan6402 2016-02-21 23:45
    关注

    Call wg.Add outside of the goroutine and pass a pointer to the wait group.

    If Add is called from inside the goroutine, it's possible for the main goroutine to call Wait before the goroutines get a chance to run. If Add has not been called, then Wait will return immediately.

    Pass a pointer to the goroutine. Otherwise, the goroutines use their own copy of the wait group.

    func jobWorker(id int, jobs <-chan string, wg *sync.WaitGroup) {
    
        defer wg.Done()
    
        for job := range jobs {
            item := ParseItem(job)
            item.SaveItem()
            MarkJobCompleted(item.ID)
            log.Println("Saved", item.Title)
        }
    }
    
    // ProcessJobs processes the jobs from the list and deletes them
    func ProcessJobs() {
    
        jobs := make(chan string)
    
        list := GetJobs()
        // Start workers
        var wg sync.WaitGroup
        for w := 0; w < 10; w++ {
            wg.Add(1)
            go jobWorker(w, jobs, &wg)
        }
    
        for _, url := range list {
            jobs <- url
        }
    
        close(jobs)
        wg.Wait()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)