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条)

相关推荐 更多相似问题