跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 求问《GO 语言圣经》里这句话应该怎么解释?
未分類
19 4 月 2021

求问《GO 语言圣经》里这句话应该怎么解释?

求问《GO 语言圣经》里这句话应该怎么解释?

資深大佬 : yuantingwuji 12

下面这段代码来自《 GO 语言圣经》 8.6. 示例: 并发的 Web 爬虫

func main() {     worklist := make(chan []string)  // lists of URLs, may have duplicates     unseenLinks := make(chan string) // de-duplicated URLs      // Add command-line arguments to worklist.     go func() { worklist <- os.Args[1:] }()      // Create 20 crawler goroutines to fetch each unseen link.     for i := 0; i < 20; i++ {         go func() {             for link := range unseenLinks {                 foundLinks := crawl(link)                 go func() { worklist <- foundLinks }() //避免死锁             }         }()     }      // The main goroutine de-duplicates worklist items     // and sends the unseen ones to the crawlers.     seen := make(map[string]bool)     for list := range worklist {         for _, link := range list {             if !seen[link] {                 seen[link] = true                 unseenLinks <- link             }         }     } } 

书里面说,“crawl 函数爬到的链接在一个专有的 goroutine 中被发送到 worklist 中来避免死锁”,就是上面代码中加了中文注释那一行,为什么不把 worklist <- foundLinks 这个操作加入专有协程就有引起死锁呢?

大佬有話說 (10)

  • 資深大佬 : iceheart

    就是单一协程程服务的模式代替临界区访问的模式

  • 資深大佬 : ihipop

    一说的高大上,单纯从代码看,中文注释处这是把结果发送给 work list,但是 worklist 可能是满的,如果读取 work list 的那边读取慢或者不读取,爬取协程就被阻塞在写入 work list chan 那边了,把发送行为放到子协程就不影响爬取协程继续爬取。

  • 資深大佬 : ihipop

    @ihipop 然后
    main 函数末尾会把所有未爬取过的连接过滤一遍,再发给 unseenlinks,如果直接把 foundlinks 发现的链接直接给 crawl 爬取,那么这里可能存在重复的链接,爬虫就会陷入死循环了。

  • 主 資深大佬 : yuantingwuji

    @ihipop 噢,我好像明白了,如果 worklist <- foundLinks 操作不加入专有协程,则会阻塞 unseenLinks 这个变量的读取,这样会进一步阻塞主协程 unseenLinks 的接收操作,进而阻塞主协程 worklist 的的读取,这样就形成死锁了。

  • 資深大佬 : yeqown

    unseenLinks 和 worklist 在阻塞情况下互相等待,就会出现死锁

  • 資深大佬 : mogg

    考虑 unseenLinks 和 worklist 都满了的情况,worklist <- foundLinks, unseenLinks <- link 都阻塞,这时候就死锁了

  • 資深大佬 : treblex

    跑个题,为啥接收命令行参数也要开个协程

  • 資深大佬 : VincentYe123

    @treblex 和上面的回答的原因一样

  • 資深大佬 : kkbblzq

    @treblex 因为是无缓冲区的 chan,原因还是和上面一样

  • 資深大佬 : hq136234303

    @ihipop 这样回照成携程的无限上涨

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具