相关疑难解决方法(0)

Go中的Webcrawler

我正在尝试在Go中构建一个Web搜寻器,我想在其中指定并发工作器的最大数量。只要队列中有要探索的链接,他们都将工作。当队列中的元素少于工作者时,工作者应该大声喊叫,但如果发现更多链接,请继续执行。

我试过的代码是

const max_workers = 6
// simulating links with int
func crawl(wg *sync.WaitGroup, queue chan int) {
    for element := range queue {   
        wg.Done() // why is defer here causing a deadlock?
        fmt.Println("adding 2 new elements ")
        if element%2 == 0 {
            wg.Add(2)
            queue <- (element*100 + 11)
            queue <- (element*100 + 33)
        }

    }
}

func main() {
    var wg sync.WaitGroup
    queue := make(chan int, 10)
    queue <- 0
    queue <- 1
    queue <- 2
    queue <- 3 …
Run Code Online (Sandbox Code Playgroud)

web-crawler go

2
推荐指数
1
解决办法
1425
查看次数

标签 统计

go ×1

web-crawler ×1