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

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。